没有@ResponseBody 的@RestController 在方法上工作不正确 [英] @RestController without @ResponseBody on methods work incorrect

查看:49
本文介绍了没有@ResponseBody 的@RestController 在方法上工作不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器:

@RestController@RequestMapping(value = "/base/url")公共类 MyController {@请求映射(value = "/child/url",方法 = RequestMethod.POST)@ResponseBody公共字符串 mmm() {返回好的";}}

现在它可以工作(服务器响应 Ok)但我认为 @ResponseBody 是多余的,因为我们使用了 @RestController 并删除了 @ResponseBody 注释

我看到以下服务器响应:

<头><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/><title>未找到错误 404</title><身体><h2>HTTP 错误 404</h2><p>访问/base/url/child/url/Ok 的问题.原因:<预>未找到

</p><小时/><我><small>Powered by Jetty://</small></i><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

你能解释一下这种行为吗?

附注

Spring 版本:4.1.6.RELEASE

附言

我发现只有这部分与 mvc config 相关:

<context:component-scan base-package="base.package"/>

解决方案

为了能够使用 @RestController,您必须显式启用新的注释处理类.通过添加 <mvc:annotation-driven/>(对于 XML)或 @EnableWebMvc(对于 Java Config).

默认情况下,DispatcherServlet 注册了现在已弃用的 AnnotationMethodHandlerAdapterDefaultAnnotationHandlerMapping,它们可以被认为是前身或更新的东西.

I have the following controller:

@RestController
@RequestMapping(value = "/base/url")
public class MyController {

    @RequestMapping(
            value = "/child/url",
            method = RequestMethod.POST
    )
    @ResponseBody
    public String mmm() {      
        return "Ok";
    }
}

Now its working(server response Ok) but I thought that @ResponseBody redundant because we use @RestController and removed @ResponseBody annotation

and I see following server response:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <title>Error 404 Not Found</title>
    </head>
    <body>
        <h2>HTTP ERROR 404</h2>
        <p>Problem accessing /base/url/child/url/Ok. Reason:

            <pre>    Not Found</pre>
        </p>
        <hr />
        <i>
            <small>Powered by Jetty://</small>
        </i>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>                                                


    </body>
</html>

Can you explain this behaviour ?

P.S.

Spring version: 4.1.6.RELEASE

P.S.

I have found only this part related to mvc config:

<context:annotation-config/>

<context:component-scan base-package="base.package"/>

解决方案

To be able to use @RestController you have to explicitly enable the new annotation processing classes. By either adding <mvc:annotation-driven /> (for XML) or @EnableWebMvc (for Java Config).

By default the DispatcherServlet registers the, now deprecated, AnnotationMethodHandlerAdapter and DefaultAnnotationHandlerMapping which can be considered the predecessor or the newer stuff.

这篇关于没有@ResponseBody 的@RestController 在方法上工作不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆