Bael-3395: Spring optional path variables (#8193)
* initial test cases * changes in @requestMapping * moving code to spring-mvc-basic-2 project
This commit is contained in:
parent
26dba08425
commit
86c72ffd7b
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
public class ArticleViewerController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/mapParam")
|
||||
public class ArticleViewerWithMapParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable Map<String, String> pathVarsMap) {
|
||||
|
||||
String articleId = pathVarsMap.get("id");
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(Integer.valueOf(articleId));
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/optionalParam")
|
||||
public class ArticleViewerWithOptionalParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Optional<Integer> optionalArticleId) {
|
||||
|
||||
if(optionalArticleId.isPresent()) {
|
||||
Integer articleId = optionalArticleId.get();
|
||||
return new Article(articleId);
|
||||
}else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/requiredAttribute")
|
||||
public class ArticleViewerWithRequiredAttributeController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id", required = false) Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/seperateMethods")
|
||||
public class ArticleViewerWithTwoSeparateMethodsController {
|
||||
|
||||
@RequestMapping(value = "/article/{id}")
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
return new Article(articleId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/article")
|
||||
public Article getDefaultArticle() {
|
||||
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.model;
|
||||
|
||||
public class Article {
|
||||
|
||||
public static final Article DEFAULT_ARTICLE = new Article(12);
|
||||
|
||||
private Integer id;
|
||||
|
||||
public Article(Integer articleId) {
|
||||
this.id = articleId;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Article [id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "org.baeldung.controller.config" })
|
||||
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "com.baeldung.controller", "org.baeldung.controller.config" })
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsNotPassed_thenResponse500() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isInternalServerError());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithOptionalParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithRequiredAttributeIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithMapParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.model.Article;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithTwoSeparateMethodsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue