[英]JAXB - Send XML with namespace to REST controller in SpringBoot
I am POSTing an xml to my springboot application and receiving it into a Pojo Jaxb and returning a response . 我正在将xml发布到我的springboot应用程序并将其接收到Pojo Jaxb并返回响应。 I am able to do this successfully , however if i pass xml with namespace i am getting response code as 406 and response body as no content .
我能够成功地做到这一点,但是如果我通过命名空间传递xml我得到响应代码为406而响应正文为无内容。 I tried various ways to add namespace in my Pojo and even tried adding it in package-info but i am unable to find a way to get it working , Please advice .
我尝试了各种方法在我的Pojo中添加命名空间,甚至尝试在package-info中添加它,但我无法找到让它工作的方法,请建议。 Below is the working example with a simple xml without namespace
下面是一个简单的xml没有命名空间的工作示例
Pojo Employee.java Pojo Employee.java
@XmlRootElement(name = "Employee")
public class Employee {
private int id;
private String name;
private float salary;
public Employee() {}
public Employee(int id, String name, float salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement //(namespace="http://www.example.org/property")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
Below is my Controller method which is getting values from the xml via POJO 下面是我的Controller方法,它通过POJO从xml获取值
@RequestMapping(value = { "/myURL" }, method = RequestMethod.POST, consumes = { "application/xml" })
public ResponseEntity<?> postMethodXMLreturnXML(@RequestBody Employee list) {
try {
Employee FirstValue=list;
System.out.println("SOP : post Method started . Name " + FirstValue.getName());
return new ResponseEntity<Object>(Employee , HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return (new ResponseEntity<String>(ErrorCodes.SERVER_ERROR.getDescription(),
HttpStatus.INTERNAL_SERVER_ERROR));
}
}
Below is the xml which i post to this and get desired output with valid response code and response message 下面是我发布到此的xml,并获得具有有效响应代码和响应消息的所需输出
Input post xml 输入帖子xml
<?xml version="1.0"?>
<Employee>
<id>1</id>
<name>myName</name>
<salary>1.1</salary>
</Employee>
But i want now to pass below xml , and when i pass below xml to this code i get response code 406 and no content , Tried adding namespace in pojo and even tried adding code to package info, Please advice Thanks in advance 但我想现在传递下面的xml,当我通过下面的xml到这个代码我得到响应代码406而没有内容,尝试在pojo中添加命名空间甚至尝试添加代码到包信息,请建议在此先感谢
<?xml version="1.0"?>
<Employee>
<myns:id>1</myns:id>
<myns:name>myName</myns:name>
<myns:salary>1.1</myns:salary>
</Employee>
The XML uses namespace prefixes, but the namespace itself is not defined in Employee's start tag. XML使用名称空间前缀,但名称空间本身未在Employee的开始标记中定义。
<?xml version="1.0"?>
<Employee xmlns:myns="http://example/a/b/c">
<myns:id>1</myns:id>
<myns:name>myName</myns:name>
<myns:salary>1.1</myns:salary>
</Employee>
Also add the namespace to each XML element. 还要将命名空间添加到每个XML元素。
@XmlElement(namespace="http://example/a/b/c")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.