避免响应中的JSON转义

时间:2018-04-02 21:16:44

标签: java json spring spring-boot jackson

我有一个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();
}

1 个答案:

答案 0 :(得分:0)

Spring HTTP Support

不需要创建响应。你做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);
}