@ResponseBody , ResponseEntity Spring return Object as JSON
@RestController
itself adds @ResponseBody
annotation. You can see it in the Github Issue
You can also check the official spring tutorials. Here you can check the below lines and examples:
These controller methods return simple POJOs -
Collection<Bookmark>
,
andBookmark
, etc., in all but theadd
case. When an HTTP request
comes in that specifies anAccept header
,Spring MVC
loops through the
configuredHttpMessageConverter
until it finds one that can convert
from thePOJO
domain model types into the content-type specified in
theAccept header
, if so configured.
You can also follow the below lines and examples from Official doc
@RestController is a stereotype annotation that combines @ResponseBody
and @Controller. More than that, it gives more meaning to your
Controller and also may carry additional semantics in future releases
of the framework.
And,
As with @RequestBody and @ResponseBody, Spring uses
HttpMessageConverter to convert from and to the request and response
streams.
Spring Boot : How to pass errors through JSON response?
RestTemplate will throw an exception for any error responses. You can surround the template.getForEntity
call in a try-catch block and handle the exceptions, returning the message in any format you want.
The exception you are looking for is HttpStatusCodeException
.
Migrating @ResponseBody to ResponseEntity
According to the javadoc for @RequestMapping (https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html), the body of the ResponseEntity will be unwrapped and written to the response output stream. So the output should be the same.
Documentation for the current release (https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types) says essentially the same thing.
How to return Bean class Object as JSON in Rest Controller in Spring MVC
add dependency in pom.xml
<dependency>
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Write one method in controller.
public String toJson(User bean) {
return new JSONSerializer().transform(new DateTransformer("MM/dd/yyyy HH:mm:ss"),java.util.Date.class).serialize(bean);
}
and pass your bean object to this method.
return new ResponseEntity<String>(toJson(bean),headers, HttpStatus.CREATED);
change return type to string in method signature public ResponseEntity<String> callRequest
NOTE: For Spring boot you can use @RestController annotation which will auto convert objects to json.
Related Topics
How to Pass List in Postman in Get Request and Get in Getmapping
How to Create a Spring JPA Repository Findby Query Using a Property That Contains a Keyword
Replacing Json String With Another String in Java
Checking If a String Contains Only Digits (Isdigitmethod Required)
Javax.Validation - Allow Null But Validate If the Value Is Not
Error Creating Bean With Name and Singleton Bean Creation Not Allowed
How to Make Multiple Line String to Single Line String
How to Prevent Xss Attacks or Untrusted Data in Rest API Json Using Java
Reading Files With Intellij Idea Ide
Is There an Invisible Character That Is Not Regarded as Whitespace
Java Classes to Generate the Given Json String
String Match of Only 3 Specific Words
Java Socket Api: How to Tell If a Connection Has Been Closed
Junit 5 - No Parameterresolver Registered for Parameter
How to Mock a Generic Parameter for a Unit Test in Java
The Package Org.W3C.Dom Is Accessible from More Than One Module: <Unnamed>, Java.Xml