Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RestController without @ResponseBody on methods work incorrect

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"/>
like image 404
gstackoverflow Avatar asked Feb 15 '16 10:02

gstackoverflow


People also ask

Is @ResponseBody required?

@SeanPatrickFloyd @RequestBody is actually still required, @ResponseBody is implicit when using @RestController .

What is true about @RestController annotation?

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.

Is ResponseBody required with RestController?

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.

What is the purpose of the @ResponseBody annotation?

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.

What is the difference between @restcontroller and @responsebody?

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.

What is @restcontroller annotation in Spring Boot?

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!

What does the @responsebody annotation tell a controller?

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:

What is the difference between @controller and @responsebody in Salesforce?

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.


1 Answers

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.

like image 96
M. Deinum Avatar answered Oct 16 '22 17:10

M. Deinum