Spring MVC 4 response body serialization works with JSON but not with XML -
i working on setting rest api spring 4. http message converters present default json & xml. try setup 2 end-points, 1 returning json & xml. json object seems returned expected when try hit xml, end 406 exception,
the resource identified request capable of generating responses characteristics not acceptable according request "accept" headers.
i have included maven dependencies both json & xml. below snippet of pom.xml,
<dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>javax.xml.bind</groupid> <artifactid>jaxb-api</artifactid> <version>${jaxb-api.version}</version> </dependency> below controller code,
@restcontroller @requestmapping(value="/employee") public class hellocontroller { @requestmapping(method=requestmethod.get , produces="application/json",value="/hello.json") public list<employee> getemployeejson(){ employee emp = new employee(); emp.setid(1); emp.setname("x"); employee emp1 = new employee(); emp1.setid(2); emp1.setname("y"); list<employee> res = new arraylist<employee>(); res.add(emp); res.add(emp1); return res; } @requestmapping(method=requestmethod.get , produces="application/xml",value="/hello.xml") public list<employee> getemployeexml(){ employee emp = new employee(); emp.setid(1); emp.setname("x"); employee emp1 = new employee(); emp1.setid(2); emp1.setname("y"); list<employee> res = new arraylist<employee>(); res.add(emp); res.add(emp1); return res; } } do share thoughts on missing here
according documentation should add jackson-dataformat-xml dependency enable response body xml serialization. in case using maven add:
<dependency> <groupid>com.fasterxml.jackson.dataformat</groupid> <artifactid>jackson-dataformat-xml</artifactid> </dependency>
Comments
Post a Comment