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 ?
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"/>
@SeanPatrickFloyd @RequestBody is actually still required, @ResponseBody is implicit when using @RestController .
Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.
The controller is annotated with the @RestController annotation; therefore, the @ResponseBody isn't required. Every request handling method of the controller class automatically serializes return objects into HttpResponse.
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.
Essentially, @RestController extends the capabilities of both the @Controller and @ResponseBody annotations. Other than the fact that @RestController exists to allow Spring controllers to be one line shorter, there aren't any major differences between the two annotations.
The @RestController annotation in Spring is essentially just a combination of @Controller and @ResponseBody. This annotation was added during Spring 4.0 to remove the redundancy of declaring the @ResponseBody annotation in your controller. That's one less annotation declaration!
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. Suppose we have a custom Response object: Next, the associated controller can be implemented:
The @ResponseBody is used with @Controller annotation, the @ResponseBody is annotated at method level whereas @Controller is annotated at class level. If we use @RestController annotation, then @ResponseBody is not needed to use.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With