Spring 3.0 + REST web service + json + xml -



Spring 3.0 + REST web service + json + xml -

i have tried lot of things , looked many places problem.

i trying utilize spring mvc 3.0 create rest web service phone call info in form of json , xml. @ moment have understood, when jackson core , mapper jars nowadays in classpath, spring automatically convert response json fine. works expected in project.

what struggling generate xml response. not sure have this. have tried various ways set method produce xml response. per understanding jdk 6 , above have jaxb implementations. when annotate pojo xml annotations produce xml, should give me xml response. reason keeps giving me json reponse. ideas? next pojo has xmlrootelement jaxb annotation.

bundle my.dto; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement(name = "mydto") public class mydto { string name; long quantity; string type; public string getname() { homecoming name; } @xmlelement public void setname(string name) { this.name = name; } public string gettype() { homecoming type; } @xmlelement public void settype(string type) { this.type = type; } public long getquantity() { homecoming quantity; } @xmlelement public void setquantity(long quantity) { this.quantity = quantity; } public mydto(string name, long quantity) { this.name = name; this.quantity = quantity; } public mydto() { super(); } } **my controller:** bundle my.package; import java.util.arraylist; import java.util.list; import my.dto.mydto; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; @controller @requestmapping("/spring") public class mycontroller { @requestmapping(value="{b}", method = requestmethod.get) @produces("application/xml") public @responsebody list<mydto> getmylist(@pathvariable string b) { mydto m1 = new mydto("my 1", long.valueof(100)); mydto m2 = new mydto("my 2", long.valueof(200)); list<mydto> list = new arraylist<mydto>(); list.add(m1); list.add(m2); homecoming list; } }

one:

yes, need @xml annotations must located in getters, have defined on setters, wrong.

second

you returning collection, xml little different, need create following

@xmlrootelement(name="list") public class jaxbmydtolist { private list<mydto> list; public jaxbmydtolist(){} public jaxbmydtolist(list<mydto> list){ this.list=list; } @xmlelement(name="item") public list<mydto> getlist(){ homecoming list; } }

three:

change from:

@requestmapping(value="{b}", method = requestmethod.get) @produces("application/xml") public @responsebody list<mydto> getmylist(@pathvariable string b) {

to

@requestmapping(value="{b}", method = requestmethod.get, produces=mediatype.application_xml_value) public @responsebody jaxbmydtolist getmylist(@pathvariable string b) {

observe:

@produces("application/xml") removed , replaced produces=mediatype.application_xml_value) list<mydto> replaced jaxbmydtolist

four:

i suggest create other @requestmapping method, 1 json , other handle xml

xml json spring spring-mvc jaxb

Comments