Reputation: 379
I am working on gateway api, with spring boot and jersey (I have used spring-boot-starter-jersey). I am suppose to return both xml and json response, it seems to work with json response but when request is made for xml I get 404. here is the code for the service
package com.quickp.services;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ResponseBody;
import com.quickp.unit;
import com.quickp.models.units;
import com.quickp.serviceclient.ApiClient;
@Path("api/units")
public class UnitsService{
private ApiClient client;
public UnitsService(ApiClient client){
this.client = client;
}
@GET
@Produces({MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Units getUnits(
@QueryParam("search") final String search,
@QueryParam("page") @DefaultValue("1") final int page) {
return client.getUnits(search, page, 10);
}
}
pom.xml has following:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
and Units.class is something like:
@Data
@JacksonXmlRootElement(localName = "units")
public class Units {
private int found;
private int display;
private boolean hasMore;
@JsonProperty("unit")
@JacksonXmlElementWrapper(useWrapping = false)
List<Unit> list;
}
and unit.class is something like:
@Data
@EqualsAndHashCode
@JacksonXmlRootElement(localName = "unit")
public class Unit {
private int id;
private String name;
private String unitType;
private String unitApp;
private String unitHomeApp;
}
(I am using lambok so I do not need to add getters and setters manually).
Thanks for all the help I am stuck with this.
Regards sajid
Upvotes: 3
Views: 1160
Reputation: 209102
The default XML provider uses JAXB, which expects JAXB annotations on your POJOS. If you want to use the Jackson XML provider, then you still need to register it, which will override the JAXB one.
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(JacksonXMLProvider.class);
// use JacksonJaxbXMLProvider if you also want JAXB annotation support
}
}
The reason for the 404, instead of the expected 500 is related to this question. This answer should solve the problem, so you get the expected error responses.
Upvotes: 3