* initial test cases * changes in @requestMapping * moving code to spring-mvc-basic-2 project
26 lines
766 B
Java
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;
|
|
}
|
|
|
|
}
|
|
|
|
} |