问题描述
我有以下控制器:
@RestController
@RequestMapping(value =/ base / url )
public class MyController {
@RequestMapping(
value =/ child / url,
method = RequestMethod.POST
)
@ResponseBody
public String mmm(){
returnOk;
}
}
现在它的工作(服务器响应 Ok
),但我认为 @ResponseBody
多余,因为我们使用 @RestController
@ResponseBody注释
,我看到以下服务器响应:
; html>
您必须显式启用新的注释处理类。通过添加
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1/>
< title>错误404未找到< / title>
< / head>
< body>
< h2> HTTP错误404< / h2>
< p>问题访问/ base / url / child / url / Ok。原因:
< pre>找不到< / pre>
< / p>
< hr />
< i>
< small>由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>你可以解释一下这个行为吗?
PS
春季版本:
4.1.6.RELEASE
PS
我只找到了与mvc config相关的部分:
< context:annotation-config />
< context:component-scan base-package =base.package/>
解决方案@RestController
< mvc:annotation-driven />
(对于XML)或@EnableWebMvc
)。
默认情况下,
DispatcherServlet
注册现在已弃用的AnnotationMethodHandlerAdapter
和DefaultAnnotationHandlerMapping
,它可以被认为是前任或更新的东西。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 annotationand 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
andDefaultAnnotationHandlerMapping
which can be considered the predecessor or the newer stuff.这篇关于@RestController没有@ResponseBody上的方法工作不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!