简体   繁体   English

如何将XML更改为JSON?

[英]How Can I Change XML to JSON?

I am creating a web service client from a WSDL, and I want to transform the XML that I get from a SOAP call, into a JSON object (as a RESTful WS). 我正在从WSDL创建Web服务客户端,并且希望将从SOAP调用中获得的XML转换为JSON对象(作为RESTful WS)。

I've tried GSON, but it insists on a TypeAdapter. 我已经尝试过GSON,但是它坚持使用TypeAdapter。 Since I have about 75 objects, I need something that is more generic. 由于我有大约75个对象,因此我需要一些更通用的东西。 I have also used Jackson, but it just sends it across unchanged as XML. 我也使用过Jackson,但是它只是以不变的XML形式发送。

NetBeans (wsimport) generates many classes, and example of which is shown below. NetBeans(wsimport)生成许多类,其示例如下所示。 How can I turn this into a JSON object? 如何将其转换为JSON对象?

  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementRef;
  import javax.xml.bind.annotation.XmlType;


  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "addressData", propOrder = {
      "addressLine1",
      "addressLine2"
  })
  public class AddressData {

      @XmlElement(required = true)
      protected String addressLine1;
      @XmlElementRef(name = "addressLine2", type = JAXBElement.class, required = false)
      protected JAXBElement<String> addressLine2;

      /**
       * Gets the value of the addressLine1 property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getAddressLine1() {
          return addressLine1;
      }

      /**
       * Sets the value of the addressLine1 property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setAddressLine1(String value) {
          this.addressLine1 = value;
      }

      /**
       * Gets the value of the addressLine2 property.
       * 
       * @return
       *     possible object is
       *     {@link JAXBElement }{@code <}{@link String }{@code >}
       *     
       */
      public JAXBElement<String> getAddressLine2() {
          return addressLine2;
      }

      /**
       * Sets the value of the addressLine2 property.
       * 
       * @param value
       *     allowed object is
       *     {@link JAXBElement }{@code <}{@link String }{@code >}
       *     
       */
      public void setAddressLine2(JAXBElement<String> value) {
          this.addressLine2 = value;
      }

  }

You may have to configure AnnotationMethodHandlerAdapter with a jackson messageConverter: 您可能必须使用jackson messageConverter配置AnnotationMethodHandlerAdapter

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper"/>
    <property name="supportedMediaTypes">
        <list>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="application"/>
                <constructor-arg index="1" value="json"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
        </list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter"/>
        </list>
    </property>
</bean>

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

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