Files
spring-boot-rest/spring-mvc-basics-2/src/main/java/com/baeldung/controller/optionalpathvars/ArticleViewerWithRequiredAttributeController.java
M-Abdelbaset a602f92f1a Bael-3395: Spring optional path variables (#8193)
* initial test cases

* changes in @requestMapping

* moving code to spring-mvc-basic-2 project
2019-11-15 15:07:27 -08:00

26 lines
766 B
Java

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;
}
}
}