我在 pom.xml 中添加了 Gson 的依赖项
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 返回一个 HashMap,我想将其转换为 JSON。所以我使用了 Gson 的 toJson 方法。现在的问题是该方法返回我 json,但我无法将它分配给响应,因为它的类型是 ResponseEntity是强制性的。 那么我应该怎么做才能通过响应返回该 json 呢? 因此我收到错误
required: org.springfremwork.http.ResponseEntity<java.lang.String>
found: java.lang.String
我知道返回类型是ResponseEntity 那么我应该如何将该 JSON 初始化为响应变量。
最佳答案
使用这个构造函数( source )怎么样:
ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)
ResponseEntity<String> response =
new ResponseEntity(gson.toString(),
new MultiValueMap(),
HttpStatus.OK);
关于java - 我应该如何将 Json 转换为 ResponseEntity<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772602/