Skip to content

Commit 49cf1e6

Browse files
BAEL-97 - simplifying code
1 parent 4989946 commit 49cf1e6

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.config;
2+
3+
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
4+
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
5+
import com.baeldung.web.controller.handlermapping.WelcomeController;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
9+
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
15+
@Configuration
16+
public class HandlerMappingDefaultConfig {
17+
18+
@Bean("/welcome")
19+
public BeanNameHandlerMappingController beanNameHandlerMapping() {
20+
return new BeanNameHandlerMappingController();
21+
}
22+
23+
@Bean
24+
public WelcomeController welcome() {
25+
return new WelcomeController();
26+
}
27+
28+
}

‎spring-mvc-java/src/test/java/com/baeldung/config/HandlerMappingPrioritiesConfig.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public class HandlerMappingPrioritiesConfig {
1818
@Bean
1919
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
2020
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
21-
beanNameUrlHandlerMapping.setOrder(0);
21+
beanNameUrlHandlerMapping.setOrder(1);
2222
return beanNameUrlHandlerMapping;
2323
}
2424

2525
@Bean
2626
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
2727
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
28+
simpleUrlHandlerMapping.setOrder(0);
2829
Map<String, Object> urlMap = new HashMap<>();
2930
urlMap.put("/welcome", simpleUrlMapping());
3031
simpleUrlHandlerMapping.setUrlMap(urlMap);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.handlermappings;
2+
3+
import com.baeldung.config.HandlerMappingDefaultConfig;
4+
import com.baeldung.config.HandlerMappingPrioritiesConfig;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.MockitoAnnotations;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.web.WebAppConfiguration;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15+
import org.springframework.web.context.WebApplicationContext;
16+
17+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
21+
22+
@RunWith(SpringJUnit4ClassRunner.class)
23+
@WebAppConfiguration
24+
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
25+
public class HandlerMappingDefaultConfigTest {
26+
27+
@Autowired
28+
private WebApplicationContext webAppContext;
29+
private MockMvc mockMvc;
30+
31+
@Before
32+
public void setup() {
33+
MockitoAnnotations.initMocks(this);
34+
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
35+
}
36+
37+
@Test
38+
public void whenDefaultConfig_thenMappedOK() throws Exception {
39+
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
40+
}
41+
}

‎spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public void setup() {
3535

3636
@Test
3737
public void whenConfiguringPriorities_thenMappedOK() throws Exception {
38-
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
38+
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("simple-url-handler-mapping")).andDo(print());
3939
}
4040
}

0 commit comments

Comments
 (0)