我正在构建REST服务Jersey
并使用Jackson
从我的模型的java类生成JSON.模型具有绝对简单的值,我认为这是最典型的情况.但是我得到了奇怪的结果:
[{\"name\":\"Nick\",\"role\":\"admin\",\"age\":\"32\",\"rating\":47}]
我期待的结果:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
我的字段源值不包含任何特殊字符.这些都很简单.
有我的Java课程.实体:
public class User { private String name; private String role; private String age; private Integer rating;
休息服务类别:
@ServiceConfig(contextName = "myContext") @Path("/myrest") public class MyRestService { private static final String JSON_CONTENT_TYPE = MediaType.APPLICATION_JSON + ";charset=UTF-8"; @Context protected HttpServletResponse response; @GET @Path("/users") @OpenTransaction @Produces({MediaType.APPLICATION_JSON}) public String findUsers(@QueryParam("department") String department) { response.setContentType(JSON_CONTENT_TYPE); PDTResponse.status(response).sendStatus(Response.Status.OK.getStatusCode()); Listusers = new ArrayList<>(); users.add(new User("Nick", "admin", "32", 47)); String jsonInString; ObjectMapper mapper = new ObjectMapper(); try { jsonInString = mapper.writeValueAsString(users); } catch (JsonProcessingException ex) { jsonInString = "thrown exception: " + ex.getMessage(); } return jsonInString; }
我试过使用@JsonRawValue
字符串属性的注释:
@JsonRawValue private String name;
但在这种情况下的结果是:
[{\"name\":Nick,\"role\":admin,\"age\":32,\"rating\":47}]
我希望:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
很明显,杰克逊以某种方式逃脱了结果json中的引用.但为什么要这样做,最重要的是如何避免呢?他们自己只是字符串!没有任何引号或特殊字符.
我用Java 7
和Jackson 2.6.1
.并Postman
测试结果.有什么想法来解决我的问题吗?