shashank
shashank

Reputation: 13

How should I convert Json to ResponseEntity<String>?

I have added Dependency for Gson in pom.xml

public ResponseEntity<String> findSearchInfo(@RequestParam String searchName)
 {
    ResponseEntity<String> response = gasRESTTemplate
                .getForEntity(uri,String.class);

    Gson gson = new Gson();
    response = gson.toJson(response);

return response;

}

uri is returning me a HashMap which I want to convert to JSON.So I have used Gson's toJson method.Now the problem is that method is returning me json but i am unable to assign it to response cause it's type is ResponseEntity which is compulsory. So what should I do to return that json by response? Because of this I am getting error

required: org.springfremwork.http.ResponseEntity<java.lang.String>
found: java.lang.String

I know return type is ResponseEntity So How should I initialize that JSON to response variable.

Upvotes: 1

Views: 10673

Answers (1)

Benoit Vanalderweireldt
Benoit Vanalderweireldt

Reputation: 2989

What about using this constructor (source) :

ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)

ResponseEntity<String> response = 
    new ResponseEntity(gson.toString(),
    new MultiValueMap(), 
    HttpStatus.OK);

Upvotes: 2

Related Questions