Announcement Announcement Module
Collapse
No announcement yet.
How to convert a Java List<Item> to XML Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to convert a Java List<Item> to XML

    Hello,

    I am having some trouble passing a Java List of type "Item" (i.e. my own class) to XML for my web services. Basically, I have specific handler methods that deal with content-type: application/xml and text/html. However, when I am using an HTTP GET from a test client, I am issuing ACCEPT: application/xml and expecting the returned resource in XML.

    I am using Spring and My handler method is given below:
    Code:
    	// One of the handlers
    
    @RequestMapping(value = "/items", method = RequestMethod.GET, headers = "accept=application/xml")
    	@ResponseBody
    	public List<Item> getAllItems(HttpServletRequest request,
    			HttpServletResponse response) {
    		if (request == null) {
    			throw new IllegalArgumentException(NULL_REQ);
    		}
    		List<Item> items = ItemDaoJpa.findAllItems();
    		audit.record(
    				this.getClass(),
    				"REST",
    				"Served request (" + request.getRemoteAddr() + "): "
    						+ items.toString());
    		return items;
    	}
    Code:
                 // Another handler method
                 	@RequestMapping(value = "/items", method = RequestMethod.GET, headers = "accept=text/html")
    	public String showAdmin(
    			HttpServletRequest request,
    			HttpServletResponse response,
    			ModelMap model,
    			@RequestParam(value = "domainId", defaultValue = "") String domainId,
    			@RequestParam(value = "nhsNo", defaultValue = "") String nhsNo,
    			@RequestParam(value = "date", defaultValue = "") String timeStamp,
    			@RequestParam(value = "tags", defaultValue = "") String tags,
    			@RequestParam(value = "data", defaultValue = "101010212012020120122020") byte[] sdsData) {
    		model.addAttribute("listAllItems", ItemDaoJpa.findAlltems());
    		model.addAttribute("editBoxDomainId", domainId);
    		model.addAttribute("editBoxNhsNo", nhsNo);
    		model.addAttribute("editBoxDate", timeStamp);
    		model.addAttribute("editBoxTags", tags);
    		model.addAttribute("editBoxdata", data);
    		return "items";
    	}
    when I am issuing a GET from my HTTP Client with Header = Accept: application/xml, it breaks down and returns a 406. I know that Spring has a default marshaller working underneath and doing all sorts of magic. I can see MyType object converted into xml as it had an @XmlRootElement annotation in the class definition. But I am not sure if I need to do anything extra to display a List<Item> in XML view

    N.B. I currently have no marshaller specified explicitly in the dependency section for my POM file and I am using Maven with SPring.

    Could anyone help me please ?

  • #2
    I think you only get xml out of the box if you have the jackson mappers in the classpath and have <mvc:annotation-driven/> which will auto register to mapper implimentations.

    Comment


    • #3
      Got it fixed now

      Idea is to use a wrapper class. Simple make it and return it from your desired handler method specified in the web controller.

      Code:
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.xml.bind.annotation.*;
      
      @XmlAccessorType(XmlAccessType.PROPERTY)
      @XmlRootElement
      public class Items {
      
      	private List<Address> items;
      	
      	public Addresses() {
      		// Default no-arg constructor - for JAXB
      	}
      	
      	public Items(List<Item> items) {
      		super();
      		this.addresses = new ArrayList<Item>(items);
      	}
                    
                    @XmlElementRef()
      	public List<Item> getItems() {
      		return items;
      	}
      }
      NOTHING ELSE NEEDS TO BE DONE (except, of course, importing the class in your existing design )
      Last edited by grizzleybear123; May 24th, 2012, 01:02 PM.

      Comment

      Working...
      X