java - JAXB 或 JAX-RS 将我的 JSON 响应中的数字用引号括起来,将它们转换为字符串。为什么这是默认行为,如何解决?

标签 java jaxb jax-rs

我目前正在研究 RESTful API。我有一个 Employee 类和一个 EmployeeResource 类。我还有一个自定义 DateAdapter,它将我的日期属性更改为长时间戳。但是,我的 JSON 响应将时间戳显示为字符串(用双引号引起来)而不是数字(不带双引号)。这是我的代码和捕获的 JSON 响应的缩写版本...

自定义日期适配器

public class DateAdapter extends XmlAdapter<Long, Date> {
    @Override
    public Date unmarshal(Long v) throws Exception {
        return new Date(Long.valueOf(v));  
    }
    @Override
    public Long marshal(Date v) throws Exception {
        return v.getTime();  
    }
}

实体类

@Entity
@javax.xml.bind.annotation.XmlRootElement
@XmlType(propOrder={"createdOn","empId"})
public class Employee implements Serializable {
    private Date createdOn;
    private Integer empId;

    @Column(nullable=false)
    @Temporal(TemporalType.TIMESTAMP)
    @XmlJavaTypeAdapter(DateAdapter.class)
    public Date getCreatedOn() {
        return createdOn;
    }
    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    @Id
    @XmlID
    public Integer getEmpId() {
        return empId;
    }
    public void setEmpId(Integer empId) {
        this.empId = empId;
    }
}

员工资源

@Path("/Employees")
@javax.xml.bind.annotation.XmlRootElement 
@XmlType(propOrder={"hateoas","employees"})
public class EmployeeResource {
    List<Employee> employees;

    public List<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Response getEmployee(@Context UriInfo ui, @PathParam("id") Integer id) {
        Session session = HibernateUtil.getSession();
        session.beginTransaction();
        Criteria criteria=session.createCriteria(Employee.class);
        criteria.add(Restrictions.eq("empId", new Integer(10150)));
        this.employees = criteria.list();
        return Response.ok(this).build();
    }
}

当前 JSON 响应

{
  "employees":{
    "createdOn":"1330915130163",
    "empId":"10150"
  }
}

预期的 JSON 响应

{
  "employees":{
    "createdOn":1330915130163,
    "empId":10150
  }
}

我假设有一些方法可以防止 JAXB 或 JAX-RS 将所有数字用引号引起来。有人可以指导我在哪里进行配置吗?

提前致谢!

编辑 #1 2012.03.07 好的,经过更多研究,我认为我的问题是使用默认的 JSONConfiguration.Notation MAPPED 。看起来 NATURAL JSONConfiguration.Notation 会得到我想要的东西。但是,我还没有找到一个明确的例子来说明如何广泛应用该应用程序。我假设我会在扩展 javax.ws.rs.core.Application 的 ApplicationConfig 类中指定它。

编辑 #2 2012.03.10 好的,经过更多研究后,我决定使用 JSON 解析器库 Jackson。它似乎是仅使用默认配置的最完整的 JSON 解决方案。默认情况下,日期被翻译成相应的时间戳并序列化为数字(不带引号)。我遇到的唯一缺点是 Jackson 目前不支持 JAXB 注释“@XmlID”和“@XmlIDREF”。由于我的数据模型中有直接的自引用(上面未显示),因此我创建了另一个问题来讨论。如果有人感兴趣click here to follow that thread...

最佳答案

注意:我是 EclipseLink JAXB (MOXy) JAXB 2 (JSR-222) 的领导和成员专家组。

您正在使用的 JAX-RS 实现可能正在使用 JAXB (JSR-222)使用类似 Jettison 的实现生成 JSON。 Jettison 提供了一个 StAX API 来与 JSON 交互,因为 StAX API 没有任何类型的 WRT 文本类型,所有简单值都被视为字符串:

要获得您正在寻找的行为,您可以使用不同的绑定(bind)解决方案。我们将此支持添加到 EclipseLink 2.4 的 MOXy 组件中:

要在 JAX-RS 环境中将 MOXy 配置为您的 JSON 绑定(bind)提供程序,您可以创建一个如下所示的 MessageBodyReader/MessageBodyWriter:

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
import javax.xml.transform.stream.StreamSource;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MOXyJSONProvider implements 
    MessageBodyReader<Object>, MessageBodyWriter<Object>{

    @Context
    protected Providers providers;

    public boolean isReadable(Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public Object readFrom(Class<Object> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {
        try {
            Unmarshaller u = getJAXBContext(type, mediaType).createUnmarshaller();
            u.setProperty("eclipselink.media-type", mediaType.toString());
            u.setProperty("eclipselink.json.include-root", false);//tiny fix
            return u.unmarshal(new StreamSource(entityStream), (Class) genericType);
        } catch(JAXBException jaxbException) {
            throw new WebApplicationException(jaxbException);
        }
    }

    public boolean isWriteable(Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public void writeTo(Object object, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException,
        WebApplicationException {
        try {
            Marshaller m = getJAXBContext(Customer.class, mediaType).createMarshaller();
            m.setProperty("eclipselink.media-type", mediaType.toString());
            m.setProperty("eclipselink.json.include-root", false);
            m.marshal(object, entityStream);
        } catch(JAXBException jaxbException) {
            throw new WebApplicationException(jaxbException);
        }
    }

    public long getSize(Object t, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    private JAXBContext getJAXBContext(Class<?> type, MediaType mediaType) 
        throws JAXBException {
        ContextResolver<JAXBContext> resolver 
            = providers.getContextResolver(JAXBContext.class, mediaType);
        JAXBContext jaxbContext;
        if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
            return JAXBContext.newInstance(type);
        } else {
            return jaxbContext;
        }
    }

}

了解更多信息

关于java - JAXB 或 JAX-RS 将我的 JSON 响应中的数字用引号括起来,将它们转换为字符串。为什么这是默认行为,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595561/

相关文章:

java - 如何加载图片?

java - DOM 解析器因具有 DOCTYPE 声明的 HTML 而卡住

java - JAXB 继承冲突 - 在子类上重新注释

java - 从 Jersey 客户端调用 post API 时出现 415 错误

java - JAX-RS:是否可以有一个外部可配置的@PATH?

java - 在 for 循环中创建多个 Google App Engine 实体

java - 为什么使用 setter 和 getter 方法

java - JAXB wrap 包装集合

java - MOXy JAXB 异常, "type"字段被视为子类鉴别器

java - @jsonview of jackson 不使用 jax-rs