Java 定制Jackson';s反序列化取决于Jersey';s请求查询参数?

Java 定制Jackson';s反序列化取决于Jersey';s请求查询参数?,java,rest,serialization,jackson,jersey-2.0,Java,Rest,Serialization,Jackson,Jersey 2.0,我在RESTAPI上使用Jackson 24.1+Jersey 2.10 为了以自定义格式解析日期,我有一个Jackson反序列化程序,如下所示: public class JsonDateSerializer extends JsonSerializer<Date>{ private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); @Override

我在RESTAPI上使用Jackson 24.1+Jersey 2.10

为了以自定义格式解析日期,我有一个Jackson反序列化程序,如下所示:

public class JsonDateSerializer extends JsonSerializer<Date>{

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String formattedDate = dateFormat.format(date);

        gen.writeString(formattedDate);
    }

}
公共类JsonDateSerializer扩展JsonSerializer{
私有静态最终SimpleDataFormat dateFormat=新SimpleDataFormat(“MM dd yyyy”);
@凌驾
public void serialize(日期、JsonGenerator gen、SerializerProvider提供程序)
抛出IOException、JsonProcessingException{
字符串formattedDate=dateFormat.format(日期);
一般书面记录(格式日期);
}
}
但是我想根据用户是否包含给定的请求头来更改反序列化日期的方式。为此,我需要访问Jersey在Jackson的反序列化程序中的HttpRequestContext对象

当Jersey的序列化器/反序列化器像这样一起使用时,是否可以在JAckson的序列化器/反序列化器中访问Jersey的上下文


谢谢

据我所知,您无法直接从反序列化程序访问HttpRequest,但是,我在对象中使用了一个标志进行了类似的操作

    @XmlElement(name = "date")
    public String getDate() {
        return (formatFlag) ? dateFormatter1.format(date): dateFormatter2.format(date);
    }
当然,formatFlag可以是@XmlTransient

EDIT

是的,ContextResolverFactory为上下文解析程序进行缓存。。。我以不同的方式工作,希望这有助于-

为日期创建XMLAdapter作为Springbean

import java.util.Date;
import javax.inject.Inject;
import javax.inject.Named;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.commons.lang3.time.DateFormatUtils;

@Named
public class RequestBasedXMLAdapter extends XmlAdapter<String, Date> {
    @Inject
    private RequestData requestData;

    @Override
    public String marshal(Date value) throws Exception {

        return DateFormatUtils.format(value, requestData.getMyDateFormat());
    }

    @Override
    public Date unmarshal(String value) throws Exception {
        // i am not interested in incoming date
        return new Date();
    }
}
为JAXBContext创建上下文解析器

  • 注入spring应用程序上下文
  • 从上下文中将XMLAdapter定义为Springbean
  • 通过修改以下方法返回定制的jaxbcontext(实现为decorator)

    public JAXBMarshaller createMarshaller() throws JAXBException {
        JAXBMarshaller marshaller = delegate.createMarshaller();
        marshaller.setAdapter(adapter);
        return marshaller;
    }
    
    public JAXBUnmarshaller createUnmarshaller() throws JAXBException {
        JAXBUnmarshaller unmarshaller = delegate.createUnmarshaller();
        unmarshaller.setAdapter(adapter);
        return unmarshaller;
    }
    
  • 现在在资源类中(您已经在其中定义了GET/POST方法)

  • 注入请求范围的bean RequestData
  • 从RequestData中的查询/标头参数设置dateformat
  • 将响应类中的日期字段注释为

     @XmlJavaTypeAdapter(RequestBasedXMLAdapter.class)
     private Date myCustomDate = new Date();
    
    现在,您应该能够根据您的请求更改日期格式

    还请注意,我使用的是Moxy


    旧答案

    我认为您可以通过返回不同的ObjectMapper来实现这一点

    试一试

  • 为ObjectMapper编写ContextResolver
  • 使用@Context注入HTTPHeader
  • 根据您的标题参数,返回使用适当日期格式的差异映射器

  • 上述方法的缺点是相同的日期格式将应用于该响应中的所有日期。

    这不起作用,因为
    ContextResolver
    是单例的,并且不会在每次请求时请求新的objectmapper。它在第一次请求后被缓存。我真的需要一个影响所有实体的全局解决方案。谢谢你!
     @XmlJavaTypeAdapter(RequestBasedXMLAdapter.class)
     private Date myCustomDate = new Date();