Spring Boot中@Controller与@RestController代码技术咨询
Hey there! Let's walk through your Spring Boot controller code, clear up how these annotations work, and address key details you might care about since your @Controller is already working fine for serving static HTML.
First, let's lay out your code properly for clarity:
@Controller Implementation
@Controller("/") public class GreetingController { @GetMapping("/greeting") public String get() { return "greeting"; } }
@RestController Implementation
@RestController("/test") public class TestRest { @RequestMapping() public List<TestDTO> get() { List<TestDTO> dtos = new ArrayList<>(); dtos.add(new TestDTO("value1","value2")); dtos.add(new TestDTO("value1","value2")); return dtos; } }
Let's break down the critical details here:
1. Core Difference Between @Controller and @RestController
@Controlleris built for MVC view rendering: When you return a string like"greeting", Spring looks for a matching view template (e.g.,src/main/resources/templates/greeting.htmlif using Thymeleaf) to render and send back as HTML. This is exactly why your current setup works for serving static-like HTML content.@RestControlleris a shortcut for@Controller + @ResponseBody: It skips view rendering entirely, directly serializing the returned object (yourList<TestDTO>) into JSON/XML and sending it as the raw response body. You don't need to add@ResponseBodyto each method when using this annotation.
2. Fixing the @RestController Path Mapping
You might have noticed your TestRest controller isn't behaving as expected (since @RestController("/test") doesn't set a path prefix like you might think). Here's the issue:
- The parameter inside
@Controlleror@RestControllersets the bean name for the controller, not the request path prefix. - To add a base path for all methods in the controller, use
@RequestMappingat the class level.
Here's the corrected TestRest code:
@RestController @RequestMapping("/test") // This sets the base path for all methods in this controller public class TestRest { @GetMapping() // Maps to GET /test public List<TestDTO> get() { List<TestDTO> dtos = new ArrayList<>(); dtos.add(new TestDTO("value1","value2")); dtos.add(new TestDTO("value1","value2")); return dtos; } }
Now when you hit GET /test, you'll get a JSON array of your TestDTO objects as expected.
3. Quick Note on Your @Controller Setup
Your @Controller("/") is also setting the bean name, not a path prefix. Since your @GetMapping("/greeting") uses an absolute path, it works fine, but if you wanted all methods in GreetingController to sit under a prefix (e.g., /greet), you'd add @RequestMapping("/greet") to the class, and the method would become @GetMapping("/greeting") (resulting in the full path /greet/greeting).
- Don't confuse the bean name parameter in
@Controller/@RestControllerwith request path prefixes—always use@RequestMapping(or its shortcuts like@GetMapping) at the class level for that. - For
@Controllerto render views, ensure you have a template engine dependency (like Thymeleaf) in your build file, and your templates are stored in the default directory (src/main/resources/templates).
内容的提问来源于stack exchange,提问作者NickJ

