我正在研究球衣服务,我在这里提到它在我返回一个java对象时工作正常.后来我试图让java对象通用它给出异常 javax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException
@XmlRootElement
public class AppObject<T> implements Serializable {
private List<T> list;
private String license;
public AppObject() {
list = new ArrayList<T>();
}
public AppObject(List<T> list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务
@GET
@Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON})
@Path("/getreq")
public AppObject<DimRequirement> savePayment() {
AppObject<DimRequirement> appObject …Run Code Online (Sandbox Code Playgroud) 我想将所有xml数据填充到java bean类中,
<employee>
<empId>a01</empId>
<dptName>dev</dptName>
<person>
<name>xyz</name>
<age>30</age>
<phone>123456</phone>
</person>
</employee>
Run Code Online (Sandbox Code Playgroud)
下面是我想要存储xml数据的java bean类.
class employee{
private String empId;
private String dptName;
private Person person;
//getter setter below
}
class Person{
String name;
private String age;
private String phone;
//getter setter below
}
Run Code Online (Sandbox Code Playgroud)
我认为这可以用JAXB但是怎么做?
注意:我还要将数据保存到数据库中.
marshaller是否有办法生成一个跳过任何null属性的新xml文件?因此someAttribute =""之类的内容不会出现在文件中.
谢谢
这是一个错误吗?
我需要nillable = "true"在xsd模式中。从我的Java代码生成此类元素的唯一方法是使用@XmlElement(nillable = true),对吗?但是在这种情况下,我将无法利用此定义,也无法检查元素是否设置为nil。该方法isNil()在JAXBElement包装器类中。
因此,我在这里有哪些选择-我想nillable = "true"在我的xsd模式中生成并能够检查它是否从我的Java代码中设置。
我遇到了问题,我使用带有xjc的架构xsd生成了类.当我运行marshal操作时,我收到以下错误:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation] at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) at prova.StampGenericXML.staticStampGenericXML(StampGenericXML.java:42) at prova.TestLauncher.main(TestLauncher.java:199) Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown …Run Code Online (Sandbox Code Playgroud) 我在生成WSDL时面临问题.我想创建生成json字符串的SOAP Web服务.下面的pojo类从另一个web项目引用到我的web服务项目中.我在servlet响应对象上编写生成的json字符串,该对象是使用WebServiceContext创建的,并使用@Resource注释进行注释.
我还尝试调试 Web服务方法(使用@WebParam进行参数化注释),pojo但是项目没有在调试模式下启动.在调用web方法之前,它会抛出所有字段的异常:
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 15 counts of IllegalAnnotationExceptions
Class has two properties of the same name "result"
this problem is related to the following location:
at public portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.getResult()
at portal.common.ejb.LoginResponse
this problem is related to the following location:
at private portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.result
at portal.common.ejb.LoginResponse
Class has two properties of the same name "userInfo"
this problem is related to the following location:
at …Run Code Online (Sandbox Code Playgroud) 早上好.今天早上,当我通过泽西实体提供商MessageBodyReader和MessageBodyWriters时,我遇到了以下问题.
我想编写一个资源方法和客户端,它返回一个自定义对象列表和媒体类型application/xml.所以我想使用JAXB(我是JAXB的新手).我能够通过编写自己的扩展MessageBodyReader和来实现这一目标MessageBodyWriter.但我害怕我跟随的方式.看看我实施的方式:
资源方法:
@Path("productlist/xml")
@GET
public RetObjects getProductsXml(){
List<Product> pList = new ArrayList<Product>();
pList.add(new Product("1","Dell latitude E6000",2900,500));
pList.add(new Product("2","Xperia Z2",549,400));
RetObjects obj = new RetObjects();
obj.setObject(pList);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
我的自定义对象:
@Entity
@Table (name="PRODUCT")
@XmlRootElement(name="product")
public class Product {
@Id
@Column(name = "CODE")
private String code;
...
// rest of the fields, constructors, getters and setters
}
Run Code Online (Sandbox Code Playgroud)
包装我的自定义对象列表的对象:
@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class RetObjects {
@XmlElement(name = "product")
private …Run Code Online (Sandbox Code Playgroud) 我正在使用JAXB来处理多个XML文档.
我的问题是我无法更改相关的xsd,我不想修改生成的类.
我想要做的是使用JAXB Binding自定义文件来实现所需的结果.
我需要做的就是执行与使用@XmlType.namespace注释相同的操作.
是否可以@XmlType.namespace通过JAXB绑定自定义文件设置注释?
我正在尝试在客户端模式下使用Jersy 2将XML发布到服务器,但我总是得到一个例外.
我的pom文件中只有一个依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.18</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的Java代码:
public static void main(String... args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080");
Entity<SimpleClass> entity = Entity.entity(new SimpleClass(), MediaType.APPLICATION_XML_TYPE);
target.request(MediaType.TEXT_XML_TYPE).post(entity);
}
@XmlRootElement(name = "test")
@XmlAccessorType(XmlAccessType.NONE)
public class SimpleClass {
@XmlElement(name = "hello")
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class jersey.SimpleClass, genericType=class jersey.SimpleClass.
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
jaxb ×10
java ×9
jersey ×3
marshalling ×2
web-services ×2
attributes ×1
jax-rs ×1
jaxbelement ×1
jersey-2.0 ×1
null ×1
overriding ×1
parent-child ×1
spring ×1
string ×1
xml ×1
xml-nil ×1