@XmlJavaTypeAdapter不起作用

时间:2013-06-12 03:16:14

标签: java jersey

我收到了错误http://jira.codehaus.org/browse/JACKSON-288,但它说错误应该已经在版本1.6.2中得到修复。

我指的是很多线程,比如,
Jersey JSON and Date
How to convert Date(ActionScript 3) to java.util.Date through a xml?

我尝试了1.12版,1.14版,1.17.1版,所有这些都无法在我这边工作。

@XmlRootElement(name="info")
@XmlAccessorType(XmlAccessType.NONE)
public class InfoVO  { 
    private int infoId;
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date createTime;
//...get/set

}

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ws.rs.WebApplicationException;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String marshal(Date v) {
        return dateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) {
        try {
            return dateFormat.parse(v);
        } catch (ParseException e) {
            throw new WebApplicationException();
        }
    }
}


    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>jsr250-api</artifactId>
        <version>1.0</version>
    </dependency>

但是DateAdapter根本无法被调用,并且有例外,

2013-06-12 11:11:13.363:WARN :: / xa / info / save / 12121:org.codehaus.jackson.map.JsonMappingException:无法从String值&构造java.util.Date的实例#39; 2013-06-08 08:00:00&#39;:不是有效的陈述(错误:无法解析日期&#34; 2013-06-08 08:00:00&#34;:与任何不兼容标准形式(&#34; yyyy-MM-dd&#39; T&#39; HH:mm:ss.SSSZ&#34;,&#34; yyyy-MM-dd&#39; T&#39; HH:mm :ss.SSS&#39; Z&#39;&#34;,&#34; EEE,dd MMM yyyy HH:mm:ss zzz&#34;,&#34; yyyy-MM-dd&#34;))|在[来源:java.io.StringReader@b0ff5e1; line:8,column:23](通过参考链:com.xchange.me.vo.InfoVO [&#34; createTime&#34;])

1 个答案:

答案 0 :(得分:0)

我找到了根本原因,因为我添加了一个自定义的MessageBodyReader,

  @Provider
    @Consumes("application/json")
    public class CustomJsonReader<T> implements MessageBodyReader<T> {
        @Override
        public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return true;
        }

    @Override
    public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
            InputStream entityStream) throws IOException, WebApplicationException {

        /*
         * Copy the input stream to String. Do this however you like. Here I use
         * Commons IOUtils.
         */
        StringWriter writer = new StringWriter();
        IOUtils.copy(entityStream, writer, "UTF-8");
        String json = writer.toString();

        /*
         * if the input stream is expected to be deserialized into a String,
         * then just cast it
         */
        if (String.class == genericType)
            return type.cast(json);

        /*
         * Otherwise, deserialize the JSON into a POJO type. You can use
         * whatever JSON library you want, here's a simply example using GSON.
         */
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, type);
    }
    }

所以当收到json数据时,总是进入readFrom方法,然后在行返回objectMapper.readValue(json,type)中抛出异常;

所以我认为根本原因是ObjectMapper.readValue忽略注释@XmlJavaTypeAdapter