Bael-3395: Spring optional path variables (#8106)

* initial test cases

* changes in @requestMapping
This commit is contained in:
M-Abdelbaset
2019-11-14 23:12:22 +02:00
committed by maibin
parent 54718d11a6
commit 274aafe823
11 changed files with 435 additions and 0 deletions
@@ -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 + "]";
}
}
@@ -0,0 +1,25 @@
package com.baeldung.web.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.web.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.web.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.web.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.web.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;
}
}