@ RestController和@Component之间的细微差别

问题描述 投票:2回答:1

尽管互联网上的每一页都说@RestController是@Component的规范。我不知道它是否必须与DispatcherServlet相关。但是,当我尝试通过在@RestController和@Component之间切换来尝试以下代码时,我看不到相同的行为:

首先我尝试使用@RestController:

@RestComponent
public class TestController {
    @RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
    public void testController() {
        System.out.println("Hello");
    }

}

我在控制台中得到以下输出:

你好

第二我尝试使用@Component + @ResponseBody:

@Component
@ResponseBody
public class TestController {
    @RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
    public void testController() {
        System.out.println("Hello");
    }
}

邮递员出错:

{
    "timestamp": 1570998345860,
    "status": 405,
    "error": "Method Not Allowed",
    "message": "Request method 'POST' not supported",
    "path": "/testController"
}

如果两个注释都相同,那么输出为何不同?

下面是@RestController和@Controller的源代码,它表明@RestController和@Controller都是@Component的规范:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

}


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
}

也许它必须与DispatcherServlet有关。分派器Servlet可能仅检查@RestController注释类中的URL。

java spring spring-boot annotations spring-annotations
1个回答
0
投票

仅仅因为@RestController@Component并不意味着您可以通过切换到较宽的@Component来实现相同的功能。即使添加了@ResponseBody,也无法实现等效的功能(不支持通过请求方法POST表示)。

@Component替换@Controller,因为@RestController@Controller + @ResponseBody。您还可以在@RestController的元注释中看到此内容,您会看到它用@Controller进行元注释,而不仅仅是@Component。反过来,@Controller@Component进行元注释。

© www.soinside.com 2019 - 2024. All rights reserved.