Java 在响应中避免JSON转义
我有一个springboot应用程序得到了一定的响应 除了Java 在响应中避免JSON转义,java,json,spring,spring-boot,jackson,Java,Json,Spring,Spring Boot,Jackson,我有一个springboot应用程序得到了一定的响应 除了responsest 它的打印格式类似于“responseStr”:“{\”版本\“:\”1243423as2 \“,”位置\“:”美洲\“}” 我希望它像下面这样 “responsest”:“{”版本“:“1243423as2”,“位置“:“美洲”}” 下面是我的响应对象和映射类 public class MyReponse implements Serializable { private int statusCode; privat
responsest
它的打印格式类似于“responseStr”:“{\”版本\“:\”1243423as2 \“,”位置\“:”美洲\“}”
我希望它像下面这样
“responsest”:“{”版本“:“1243423as2”,“位置“:“美洲”}”
下面是我的响应对象和映射类
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();
}
@RestController
@产生(MediaType.APPLICATION_JSON)
@刷新范围
公共类MyService{
@RequestMapping(path=“/get”,method=RequestMethod.get)
公共MyResponse GetResponse(
@RequestParam多值映射requestParameterMap
) {
//mylogic
返回(myResponse)this.buildResponse(Status.OK,myResponse).getEntity();
}
公共静态最终响应buildResponse(状态、对象实体){
返回Response.status(status).entity(entity.build();
}
不需要创建响应。您可以创建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);
}
@RestController
公共类采样控制器{
@RequestMapping(path=“/get”,method=RequestMethod.get)
@应答器
公共字符串makeJson(@RequestParam MultiValueMap){
//有些事。。。
ObjectMapper mapper=新的ObjectMapper();
返回mapper.writeValueAsString(map);
}
不清楚您为什么要创建一个响应
对象,然后在构建它时立即取出传递给它的实体。