How to call a spring restful web service to consume and produce XML as request and response using a Pojo class.
1.I have a Pojo class called as ItemDTO,which looks like this to support xml as input to the service
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "ITEM_STOCK")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDTO implements Serializable {
private static final long serialVersionUID = 4520881241472376875L;
@XmlElement
private String item_name;
@XmlElement
private String item_desl;
@XmlElement
private int item_count;
//Setters and Getters of the variables above.
}
2.Spring rest Controller
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ims.xml.DTO.ItemDTO;
@Controller
@RequestMapping(value="ims")
public class InventoryHomeController {
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public @ResponseBody ItemDTO home(Locale locale, Model model) {
model.addAttribute("serverTime", formattedDate);
ItemDTO i = new ItemDTO("renolds", "ballpointpen", 10);
return i;
}
}
OUT PUT IS:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ITEM_STOCK>
<item_name>renolds</item_name>
<item_desl>ballpointpen</item_desl>
<item_count>10</item_count>
</ITEM_STOCK>
1.I have a Pojo class called as ItemDTO,which looks like this to support xml as input to the service
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "ITEM_STOCK")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDTO implements Serializable {
private static final long serialVersionUID = 4520881241472376875L;
@XmlElement
private String item_name;
@XmlElement
private String item_desl;
@XmlElement
private int item_count;
//Setters and Getters of the variables above.
}
2.Spring rest Controller
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ims.xml.DTO.ItemDTO;
@Controller
@RequestMapping(value="ims")
public class InventoryHomeController {
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public @ResponseBody ItemDTO home(Locale locale, Model model) {
model.addAttribute("serverTime", formattedDate);
ItemDTO i = new ItemDTO("renolds", "ballpointpen", 10);
return i;
}
}
OUT PUT IS:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ITEM_STOCK>
<item_name>renolds</item_name>
<item_desl>ballpointpen</item_desl>
<item_count>10</item_count>
</ITEM_STOCK>
No comments:
Post a Comment