REST response with JAXRS and JAXB configuration

gulfam

New Member
I'm trying to develop a REST API and the responses that I want are like: in JSON:\[code\]{ locations: [{ id:1, name: "testName1"}, { id:2, name: "testName2"}, ...]}\[/code\]in XML:\[code\]<locations> <location> <id>1</id> <name>testName1</name> <location> <location> <id>2</id> <name>testName2</name> <location> ...<locations>\[/code\]The object Location I have and I would like convert is: \[code\]@Getter@Setter@XmlRootElement(name = "location")@XmlAccessorType(XmlAccessType.FIELD)@AllArgsConstructor@NoArgsConstructorpublic class Location{ @XmlElement(required = true, name = "id") private Long id; @XmlElement(required = true, name = "name") private String name;}\[/code\]In my pom.xml have:\[code\] <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.9</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxb-api</artifactId> <version>${javax.xml.version}</version> <exclusions> <exclusion> <artifactId>activation</artifactId> <groupId>javax.activation</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>${com.sun.xml.bind-jaxb-impl}</version> </dependency> ... </dependencies>\[/code\]The service I developed is:\[code\]@Path("/test/")@Servicepublic class TestService { @GET @Path("/test") @Produces({ "application/xml", "application/json" }) public List<Location> toTest() throws Exception { List<Location> locations = new ArrayList<Location>(); locations.add(new Location(1, "testName1")); locations.add(new Location(2, "testName2")); return locations; }}\[/code\]Right now, I'm getting this:JSON:\[code\]{ location: [{ id:1, name: "testName1"}, { id:2, name: "testName2"}, ...]}\[/code\]Location and not locations!XML: the same result that I want.
 
Top