Reputation: 276
I'm trying to get only the response Body in format JSON from a Spring Rest Controller, my code is :
@ResponseBody
@RequestMapping(value = "/userDetails", method = RequestMethod.GET, produces = "application/json")
public UserInfo getUserDetails(Principal principal){
return users.get(principal.getName());
}
my Result is :
Response {_body: "{id: 001}", status: 200, ok: true, statusText: "OK", headers: Headers, …}.
how can i get only the body as Json thank's ....
Upvotes: 2
Views: 791
Reputation: 3763
Use @RestController
@RestController
public class DemoController {
@GetMapping("/userDetails")
public UserInfo getUserDetails(Principal principal){
return users.get(principal.getName());
}
}
output:
{"id": 001}
Note:
Spring 4.0 introduced @RestController
, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller
and @ResponseBody
annotations. By annotating the controller class with @RestController
annotation, you no longer need to add @ResponseBody
to all the request mapping methods. The @ResponseBody
annotation is active by default. Click here to learn more.
@RestController = @Controller + @ResponseBody
Upvotes: 3
Reputation: 86
@RestController public class DemoController {
@RequestMapping(method = RequestMethod.POST,
value = "userdetails",
consumes = "application/json",
produces = "application/json")
public UserInfo getUserDetails(@RequestBody Principal principal){
return users.get(principal.getName());
}
}
other way work around
Upvotes: 0