2

am getting both xml and json format response when am using genericEntity. But don't know how to formate those xml in particular format using using genericEntity. I want response format like below

XML Format

<response><list><item><name>earth</name></item><item><name>mars</name></item></list></response>

JSON Format

{"list": [{"name":"earth"},{"name":"mars"}]}

java POJO

public class Planet {
    public String name;    
    public Planet() {
    }    
    public Planet(String name) {
    this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Controller code

@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Response getSellersBySellerCodeDet() {
    ArrayList<Planet> list = new ArrayList<Planet>();
    list = DAO.getName();
    return Response.ok(new GenericEntity<List<Planet>>(list) {}).build();
}

Response what am getting

<?xml version="1.0" encoding="UTF-8"?>
<Planets>
  <Planet>
   <name>earth</name>
  </Planet>
  <Planet>
   <name>mars</name>
  </Planet>
</Planets>

Can any one tell me how to overcome this response and getting proper response what i want. and i tried @XmlRootElment and am using dropwizard 1.2.1 and jersey 2.21 and jdbi 2

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • You can do it by creating a wrapper class (instead of `GenericEntity`) which contains the list of planets, and using JAXB annotations on it: `@XmlElementWrapper`, `@XmlElement`. See the linked question for details. – Alex Shesterov Sep 19 '18 at 10:41
  • @AlexShesterov thanks for your suggestion. its working fine without using genericEntity. but in json response {"list":{"item": [{"name":"earth"},{"name":"mars"}]}}. – jayaprakash jp Sep 19 '18 at 12:53

0 Answers0