@XmlJavaTypeAdapter不起作用 [英] @XmlJavaTypeAdapter doesn't work

查看:71
本文介绍了@XmlJavaTypeAdapter不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误 http://jira.codehaus.org/browse/JACKSON-288 ,但它表示该错误应已在1.6.2版中修复.

I got the bug http://jira.codehaus.org/browse/JACKSON-288, but it said the bug should have been fixed in the version 1.6.2.

我指的是很多线程,例如,
Jersey JSON和日期
如何转换日期(ActionScript 3)通过xml转换为java.util.Date?

I refer to lots of threads, like,
Jersey JSON and Date
How to convert Date(ActionScript 3) to java.util.Date through a xml?

我尝试了1.12、1.14、1.17.1版本,但都无法在我这边工作.

I tried version 1.12, 1.14, 1.17.1, all can't works in my side.

@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根本无法调用,并且发生了异常,

But that the DateAdapter can't be called at all, and got a exception,

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

2013-06-12 11:11:13.363:WARN::/xa/info/save/12121: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2013-06-08 08:00:00': not a valid representation (error: Can not parse date "2013-06-08 08:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))| at [Source: java.io.StringReader@b0ff5e1; line: 8, column: 23] (through reference chain: com.xchange.me.vo.InfoVO["createTime"])

推荐答案

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

I got the root cause, as I added a customized 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方法,然后在该行中抛出异常return objectMapper.readValue(json,type);

So when received the json data, always come into the readFrom method, and then threw exception in the line return objectMapper.readValue(json, type);

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

So I think the root cause is ObjectMapper.readValue ignore the annotation @XmlJavaTypeAdapter

这篇关于@XmlJavaTypeAdapter不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆