我有一个springboot应用程序获得了一定的响应
除了responseStr
其打印方式为 "responseStr":"{\"version\":\"1243423as2\",\"location\":\"Americas\"}"
我希望它像下面的
"responseStr":"{"version":"1243423as2","location":"Americas"}"
以下是我的Response对象和映射类。
public class MyReponse implements Serializable {
private int statusCode;
private String responseStr;
//getters and setters
}
我的请求映射如下
@RestController
@Produces(MediaType.APPLICATION_JSON)
@RefreshScope
public class MyService {
@RequestMapping(path = "/get", method = RequestMethod.GET)
public MyReponse getReponse(
@RequestParam MultiValueMap<String, String> requestParameterMap
) {
//mylogic
return (MyReponse) this.buildResponse(Status.OK, myResponse).getEntity();
}
public static final Response buildResponse(Status status, Object entity) {
return Response.status(status).entity(entity).build();
}
答案 0 :(得分:0)
不需要创建响应。你做json或集中你的逻辑。
这是示例代码:
样本控制器。
@RestController
public class SampleController {
@RequestMapping(path = "/get", method = RequestMethod.GET)
@ResponseBody
public String makeJson(@RequestParam MultiValueMap<String, String> map) {
// something...
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(map);
}