使用 JAXB 将 JSON 编组/解组为 Java 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1938428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:36:59  来源:igfitidea点击:

Marshall/Unmarshall a JSON to a Java class using JAXB

javajsonjaxbjax-rs

提问by user235805

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 documentationJAX-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 Hymanson libraries?

我是否需要为此创建自己的 MessageBodyReader,或者框架支持而不使用 Hymanson 库?

回答by djna

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 Hymanson). I wrote up the steps I took here

我一直在使用 Apache Wink,为此我需要使用 JSON 提供程序,例如 Jettison(一位同事一直在使用 Hymanson)。我写了我在这里采取的步骤

My guess is that you too will need to to use a JSON provider. Is there a reason not to use a Hymanson provider?

我的猜测是您也需要使用 JSON 提供程序。是否有理由不使用 Hymanson 提供商?

回答by Mark Lutton

I have been doing it successfully in RESTEasy. I have it set up to consume and produce both XML and JSON. Here is a request handler:

我一直在 RESTEasy 中成功地做到了。我已将其设置为使用和生成 XML 和 JSON。这是一个请求处理程序:

@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, i.e. it has a default constructor and it has setters and getters that the marshaller finds. 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 是一个JavaBean,即它有一个默认构造函数,并且它有编组器找到的setter 和getter。我在 ReqData 中没有任何特殊的 JSON 标记,但我在 XML 编组器的顶部有 @XmlRootElement(name="data") 和 setter 上的 @XmlElement 标记。

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”。

回答by newbiestacker

Marshalling to XML is easy, but it took me a while to figure out how to marshall to JSON. Pretty simple after you find the solution though.

编组到 XML 很容易,但我花了一段时间才弄清楚如何编组到 JSON。找到解决方案后很简单。

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();
}