You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Your Controller & RestController Code

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;
    }
}
Key Clarifications & Fixes

Let's break down the critical details here:

1. Core Difference Between @Controller and @RestController

  • @Controller is 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.html if using Thymeleaf) to render and send back as HTML. This is exactly why your current setup works for serving static-like HTML content.
  • @RestController is a shortcut for @Controller + @ResponseBody: It skips view rendering entirely, directly serializing the returned object (your List<TestDTO>) into JSON/XML and sending it as the raw response body. You don't need to add @ResponseBody to 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 @Controller or @RestController sets the bean name for the controller, not the request path prefix.
  • To add a base path for all methods in the controller, use @RequestMapping at 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).

Common Pitfalls to Avoid
  • Don't confuse the bean name parameter in @Controller/@RestController with request path prefixes—always use @RequestMapping (or its shortcuts like @GetMapping) at the class level for that.
  • For @Controller to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:22:48