小编Gin*_*g3r的帖子

适用于Jackson的RESTEasy和ContextResolver <ObjectMapper>

我的RESTEasy JSON响应的自定义问题.

web.xml我使用自动扫描:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

这是我的自定义类ObjectMapper,(我设置了非空字段和新的人类可读日期):

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {
    private Logger log = Logger.getLogger(PropertiesConfig.LOG_CATEGORY);
    private ObjectMapper objectMapper;  

    public JacksonConfig() throws Exception  {  

        objectMapper = new ObjectMapper();
        objectMapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
        objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));  
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    @Override
    public ObjectMapper getContext(Class<?> arg0) {
        return objectMapper;
    }  
}
Run Code Online (Sandbox Code Playgroud)

这是我的servlet:

@Path("/search")
public class Search extends ArtesAPI {

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public Response search(@Context HttpServletRequest request){
        RequestManager reqManager = new RequestManager(request);
        MyResponse response = reqManager.doSearchRequest();
        return Response.status(200).entity(response).build(); …
Run Code Online (Sandbox Code Playgroud)

java json response resteasy jackson

5
推荐指数
2
解决办法
6961
查看次数

标签 统计

jackson ×1

java ×1

json ×1

response ×1

resteasy ×1