How to Get Response as Json With Responseentity in Java

ResponseEntity do not return json response for Post Method

You have A_VALUE and B_VALUE as attributes in your class whereas you're setting values of a_value and b_value.

Do Autowire your PostService class and also remove these(Jackson) dependencies when you have starter-web dependency already. I would also recommend you to use Lombok's @Data and @NoArgsConstructor annotations at class level.

How can I get the json file from ResponseEntity?

Change the Response type from getForObject to String.class, then, use the BufferedWriter to write the file.

RestTemplate rest = new RestTemplate();
String response = rest.getForObject("http://example.com/", String.class);

BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
writer.append(response);
writer.close();

Edit

The RestTemplate#exchange returns a ResponseEntity<T>, not a String.

In your case, it will return a ResponseEntity<String>.

That's why you're unable to write the object. To do so, you need to get the body from the ResponseEntity object. Use the method ResponseEntity#getBody

Something like this:

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class, 1);

BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.json", true));
// here you are getting the String with the response.getBody() method,
// so buffered writer can write the file
writer.append(response.getBody());
writer.close();

Spring Rest return a JSON response with a certain http response code

How I do it

Here is how I do JSON returns from a Spring Handler method.
My techniques are somewhat out-of-date,
but are still reasonable.

Configure Jackson
Add the following to the spring configuration xml file:

<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>

With that,
Spring will convert return values to JSON and place them in the body of the response.

Create a utility method to build the ResponseEntity
Odds are good that you will have multiple handler methods.
Instead of boilerplate code,
create a method to do the standard work.
ResponseEntity is a Spring class.

protected ResponseEntity<ResponseJson> buildResponse(
final ResponseJson jsonResponseBody,
final HttpStatus httpStatus)
{
final ResponseEntity<ResponseJson> returnValue;

if ((jsonResponseBody != null) &&
(httpStatus != null))
{
returnValue = new ResponseEntity<>(
jsonResponseBody,
httpStatus);
}

return returnValue;
}

Annotate the handler method

@RequestMapping(value = "/webServiceUri", method = RequestMethod.POST)

you can also use the @PostMethod annotation

@PostMethod("/webServiceUri")

Return ResponseEntity from the handler method
Call the utility method to build the ResponseEntity

public ResponseEntity<ResponseJson> handlerMethod(
... params)
{
... stuff

return buildResponse(json, httpStatus);
}

Annotate the handler parameters
Jackson will convert from json to the parameter type when you use the @RequestBody annotation.

public ResponseEntity<ResponseJson> handlerMethod(
final WebRequest webRequest,
@RequestBody final InputJson inputJson)
{
... stuff
}

A different story

You can use the @JsonView annotation.
Check out the Spring Reference for details about this.
Browse to the ref page and search for @JsonView.

Spring Boot: Testing ResponseEntity Containing Java Time

Explanation

Here you are instantiating the timestamp field at two different times and comparing them. They will always fail to match.

Solution

I would recommend avoiding the instantiation of new ApiError(HttpStatus.BAD_REQUEST) in your test and simply verifying each field individually. For the timestamp field, just verify that it is set to a non-null value or if you want to get pedantic, verify that it has a value within the last X number of seconds instead of the precise millisecond validation that you are currently checking.

Alternative Solution (Hacky Workaround)

If you want to continue validation a JSON string against a JSON string, simply convert the response body into an object and steal it's timestamp into your comparison object.



Related Topics



Leave a reply



Submit