简体   繁体   English

使用JAXB编组/将JSON编组到Java类

[英]Marshall/Unmarshall a JSON to a Java class using JAXB

I am successfully marshaling a POJO into JSON using JAX-RS and JAXB annotations. 我已成功使用JAX-RS和JAXB批注将POJO编组为JSON。

The problem is that when I am trying to use the same for un-marshalling my request it doesn't work. 问题是,当我尝试使用相同的内容来对我的请求进行编组时,它不起作用。 As far as I can see in the documentation JAX-RS can automatically marshal and unmarshal application/json strings back to java classes. 据我在文档中所看到的,JAX-RS可以自动将应用程序/ json字符串编组和解编回到Java类。

Do I need to create my own MessageBodyReader for that, or this is supported by the framework without using Jackson libraries? 我是否需要为此创建自己的MessageBodyReader,或者框架不使用Jackson库而支持它?

Marshalling to XML is easy, but it took me a while to figure out how to marshall to JSON. 编组到XML很容易,但是花了我一段时间才弄清楚如何编组到JSON。 Pretty simple after you find the solution though. 找到解决方案后非常简单。

public static String marshalToXml( Object o ) throws JAXBException {

    StringWriter writer = new StringWriter();
    Marshaller marshaller = JAXBContext.newInstance( o.getClass() ).createMarshaller();
    marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    marshaller.marshal( o, writer );
    return writer.toString();
}

public static String marshalToJson( Object o ) throws JAXBException {

    StringWriter writer = new StringWriter();
    JAXBContext context = JSONJAXBContext.newInstance( o.getClass() );

    Marshaller m = context.createMarshaller();
    JSONMarshaller marshaller = JSONJAXBContext.getJSONMarshaller( m );
    marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    marshaller.marshallToJSON( o, writer );
    return writer.toString();
}

I have been doing it successfully in RESTEasy. 我在RESTEasy中已经成功地做到了。 I have it set up to consume and produce both XML and JSON. 我将其设置为使用和产生XML和JSON。 Here is a request handler: 这是一个请求处理程序:

@POST
@Produces(["application/json","application/xml"])
@Consumes(["application/json","application/xml"])
@Path("/create")
public Response postCreate(
         ReqData reqData) {
   log.debug("data.name is "+ data.getName());
   ...
   return Response.status(Response.Status.CREATED)
     .entity(whatever)
     .location(whateverURI)
     .build();

}

ReqData is a JavaBean, ie it has a default constructor and it has setters and getters that the marshaller finds. ReqData是JavaBean,即它具有默认构造函数,并且具有编组程序查找的setter和getter。 I don't have any special JSON tags in ReqData, but I do have @XmlRootElement(name="data") at the top for the XML marshaller and @XmlElement tags on the setters. 我在ReqData中没有任何特殊的JSON标记,但是在setter上的XML marshaller和@XmlElement标记的顶部确实有@XmlRootElement(name =“ data”)。

I use separate beans for input and output, but as far as I know you can use the same bean. 我使用单独的bean进行输入和输出,但是据我所知您可以使用相同的bean。

The client program sends the JSON string in the entity-body of the request, and sets the Context-Type and Accept headers both to "application/json". 客户端程序在请求的实体主体中发送JSON字符串,并将Context-Type和Accept标头都设置为“ application / json”。

I've been working with Apache Wink and for that I have needed to use a JSON provider, such as Jettison (a colleague has been using Jackson). 我一直在使用Apache Wink,为此,我需要使用JSON提供程序,例如Jettison(一个同事一直在使用Jackson)。 I wrote up the steps I took here 我写下了我在这里采取的步骤

My guess is that you too will need to to use a JSON provider. 我的猜测是,您也将需要使用JSON提供程序。 Is there a reason not to use a Jackson provider? 有没有不使用Jackson提供程序的原因?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM