我正在尝试创建一个基于Spring 4的restful服务。服务应该使用json和xml。我创建了一个xsd,并使用xjc将xsd编译成绑定类。
这里是xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="ServicesRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="activityType" type="xsd:int"/>
<xsd:element ref="ServicesInput" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ServicesInput">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="0"/>
<xsd:element ref="ListOfValues" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ListOfValues">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Value" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
以下是运行xjc后的java类:
服务请求。JAVA
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2016.08.13 at 12:54:09 PM EDT
//
package org.acme.foo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="activityType" type="{http://www.w3.org/2001/XMLSchema}int"/>
* <element ref="{}ServicesInput" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"activityType",
"servicesInput"
})
@XmlRootElement(name = "ServicesRequest")
public class ServicesRequest
implements Serializable
{
private final static long serialVersionUID = 1L;
protected int activityType;
@XmlElement(name = "ServicesInput")
protected List<ServicesInput> servicesInput;
/**
* Gets the value of the activityType property.
*
*/
public int getActivityType() {
return activityType;
}
/**
* Sets the value of the activityType property.
*
*/
public void setActivityType(int value) {
this.activityType = value;
}
/**
* Gets the value of the servicesInput property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the servicesInput property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getServicesInput().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link ServicesInput }
*
*
*/
public List<ServicesInput> getServicesInput() {
if (servicesInput == null) {
servicesInput = new ArrayList<ServicesInput>();
}
return this.servicesInput;
}
}
服务输出。JAVA
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2016.08.13 at 12:54:09 PM EDT
//
package org.acme.foo;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element ref="{}ListOfValues" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"listOfValues"
})
@XmlRootElement(name = "ServicesInput")
public class ServicesInput
implements Serializable
{
private final static long serialVersionUID = 1L;
protected String name;
@XmlElement(name = "ListOfValues")
protected ListOfValues listOfValues;
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the listOfValues property.
*
* @return
* possible object is
* {@link ListOfValues }
*
*/
public ListOfValues getListOfValues() {
return listOfValues;
}
/**
* Sets the value of the listOfValues property.
*
* @param value
* allowed object is
* {@link ListOfValues }
*
*/
public void setListOfValues(ListOfValues value) {
this.listOfValues = value;
}
}
ListOfValues.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2016.08.13 at 12:54:09 PM EDT
//
package org.acme.foo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Value" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
@XmlRootElement(name = "ListOfValues")
public class ListOfValues
implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlElement(name = "Value")
protected List<String> value;
/**
* Gets the value of the value property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the value property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getValue().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getValue() {
if (value == null) {
value = new ArrayList<String>();
}
return this.value;
}
}
控制类
package com.journaldev.spring.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.acme.foo.ServicesRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.journaldev.spring.model.Employee;
/**
* Handles requests for the Employee service.
*/
@RestController
public class EmployeeController {
@RequestMapping(value = "/bcreate", method = RequestMethod.POST, consumes = "application/xml")
public String createBatch(@RequestBody ServicesRequest serviceRequest) {
try {
if (serviceRequest.getServicesInput().size()==0) {
System.out.println("input is null");
}
} catch (Exception e) {
}
return "test";
}
}
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
mvc调度servlet。xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.journaldev.spring.controller" />
<mvc:annotation-driven/>
</beans>
发帖请求:
<ServicesRequest>
<activityType>1234512340</activityType>
<ServicesInput>
<name>MyName</name>
<ListOfValues>
<Value>Thisisatest</Value>
</ListOfValues>
</ServicesInput>
</ServicesRequest>
图:eclipse调试断点
这些是罐子
所以我的问题是,在上面列出的输入中,为什么我不能在控制器中看到相同的内容。输入确认到xsd。
非常感谢你的帮助。
我必须删除Jackson dataformat xml。jar从类路径中删除。然后它开始使用jaxb绑定,并正确地开始绑定。
问题内容: 我正在寻找有关如何绑定到AngularJS中的服务属性的最佳实践。 我已经研究了多个示例,以了解如何绑定到使用AngularJS创建的服务中的属性。 下面有两个关于如何绑定到服务中的属性的示例。他们都工作。第一个示例使用基本绑定,第二个示例使用$ scope。$ watch绑定到服务属性 当绑定到服务中的属性时,这些示例是首选还是建议使用其他我不知道的选项? 这些示例的前提是服务应每5
我有一些代码,其中多个方法使用键盘,并在主方法中连续调用。我正在做的练习特别要求使用4种不同的方法,所以我不能把它们放在一起。最初,我用键盘。在每个方法的末尾关闭(),但当第二个方法运行时,无论调用顺序如何,这都会导致NoTouchElementException。通过卸下键盘。close(),代码现在可以工作了,但是我现在收到了资源泄漏的警告,因为键盘没有关闭。有人能告诉我一种关闭输入而不出错的
我正在用vue创建一个电影数据库应用程序,并试图输出一个从API获取的图像源。由于某些原因,src的输出没有在应用程序中呈现出应有的效果。它打印字符串{{movie.Poster}。我显然没有以正确的方式使用v-bind。那么,关于如何以正确的方式打印海报路径,有什么想法吗?
本文向大家介绍详解python方法之绑定方法与非绑定方法,包括了详解python方法之绑定方法与非绑定方法的使用技巧和注意事项,需要的朋友参考一下 写在之前 在 Python 的类里面除了属性之外,还有方法,当然也有文档和注释这类东西,但是这个只是人来看,计算机则不关心。我们之前说过,我们一般用实例调用方法,既然我们说了是一般,那么就说明还有其他调用方法的方式,今天我们就来说一下「绑定方法和非绑定
问题内容: 我有一个Json响应,如下所示: 我不知道参数的名称和数量。因此,我需要并且想要将所有这些参数绑定到由<“ paramX”,“ valueX”>元组组成的java.util.Map字段。为此,我尝试了以下代码,但“ parametersMap”字段返回null。 如何使用JAXB批注实现这种绑定? 提前致谢。 问题答案: 基本上,您需要一个xml适配器。您可以摆弄KeyValue类上的
我使用泽西API进行我的REST服务。我的问题是:是否有更优雅的方式以JSON形式返回异常?最好是自己创建一个json对象并将其直接附加到响应中? 这是服务中一种方法的简化示例。如您所见,我使用HashMap只是因为该方法可能引发异常,在这种情况下,我需要返回有关它的信息。