1

I have a spring boot application in which I am creating a controller with @Controller annotation, when I tried to access the resource using the url it is showing me 404, "not found". But as soon as I add annotation @ResponseBody at class level or method level, it gives me desired output. I want to understand the role of @Responsebody here why it is affecting the url identification. As far as I know @ResponseBody deals with the responses.

When I have code like below -

@Controller
@RequestMapping(value = "test")

public class TestController {

    @RequestMapping(value = "/m1")
    public @ResponseBody String testMethod(){
        return "Hello First Application";
    }
}

It is giving me an exact output as Hello First Application

when I remove @ResponseBody from the method testMethod() it gives me the following output.

{
    "timestamp": "2019-06-13T06:36:14.510+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/test/m1"
}

I am expecting a string as a response.

pritampanhale
  • 106
  • 10
  • Without `@ResponseBody` the `String` is interpreted as the name of a web page to render, with it it is used as is. – M. Deinum Jun 13 '19 at 07:03
  • This error seems to be ambiguous and I think in response there should be a different error messages so that the developer would understand. At first glance it looks like, there is no mapping specified for this url. After reading many articles it was clear to me. Still there are some issues I am facing in spring boot application to fetch the proper view which I will fix. But I think the error message should be more generous than the current one. – pritampanhale Jun 13 '19 at 09:24
  • What is wrong with the error, the view is a URL as well, hence a 404 is perfectly acceptable. The view cannot be found, if you check your logs (you might need to enable debug) you will see this. – M. Deinum Jun 13 '19 at 09:50
  • Yes, i just set enabled the debug and now it is pretty much clear what is happening. thanks @M.Deinum – pritampanhale Jun 13 '19 at 10:20

2 Answers2

0

This link might explain what you need: https://www.baeldung.com/spring-request-response-body

Basically, the annotation tells the controller that the object is well formatted. @Controller is usually used for returning views, and @RestController is used for JSON, and no longer needs @ResponseBody.

Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
Claudiu Guja
  • 290
  • 1
  • 3
  • 12
  • Thanks for the link Claudiu, but it only specifies that @Responsebody gives the JSON representation of the response, but 404 says, the requested URL is not found. The question still remains that we are changing the type of response, how should it is affecting the URL identification. – pritampanhale Jun 13 '19 at 07:08
0

This depends on your view resolution. Just verify what you have specifed as default view resolution for your project.

Without ResponseBody, spring is trying to resolve your view matching string "hello ..." which is not present.

If ResponseBody is used, the return is serialized and written to the HTTP response Refer https://www.baeldung.com/spring-request-response-body

Yogesh Patil
  • 908
  • 4
  • 14