Reputation: 1402
I am POSTing an xml to my springboot application and receiving it into a Pojo Jaxb and returning a response . 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 . 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 . Below is the working example with a simple xml without namespace
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
@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
Input post 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 version="1.0"?>
<Employee>
<myns:id>1</myns:id>
<myns:name>myName</myns:name>
<myns:salary>1.1</myns:salary>
</Employee>
Upvotes: 4
Views: 4694
Reputation: 6577
The XML uses namespace prefixes, but the namespace itself is not defined in Employee's start tag.
<?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.
@XmlElement(namespace="http://example/a/b/c")
Upvotes: 1