Reputation: 4334
I have a cxf JAX-RS service which looks something like the one below. When I submit a request with requested type "application/xml" I would expect that cxf automatically converts my return value into xml. This works for the method getData
, but not for the other 2 methods. The other 2 methods return a simple String representation of the return value such as 2.0
or true
. How do I get cxf to return a XML document for all 3 methods?
@WebService
@Consumes("application/xml")
@Produces("application/xml")
public interface MyServiceInterface {
final static String VERSION = "2.0";
@WebMethod
@GET
@Path("/version")
String getVersion();
@WebMethod
@GET
@Path("/data/{user}")
Data[] getData(@PathParam("user") String username) throws IOException;
@WebMethod
@GET
@Path("/user/{user}")
boolean doesUserExist(@PathParam("user") String username);
}
Upvotes: 2
Views: 1437
Reputation: 137567
The issue is that neither String
nor boolean
has a natural representation as an XML document; XML requires an outer element, and neither CXF nor JAXB (the XML binding layer) knows what it should be.
The simplest method is to return the basic type inside a little JAXB-annotated wrapper:
@XmlRootElement
public class Version {
@XmlValue
public String version;
}
@XmlRootElement
public class UserExists {
@XmlValue
public boolean exists;
}
@WebService
@Consumes("application/xml")
@Produces("application/xml")
public interface MyServiceInterface {
final static String VERSION = "2.0";
@WebMethod
@GET
@Path("/version")
// TYPE CHANGED BELOW!
Version getVersion();
@WebMethod
@GET
@Path("/data/{user}")
Data[] getData(@PathParam("user") String username) throws IOException;
@WebMethod
@GET
@Path("/user/{user}")
// TYPE CHANGED BELOW!
UserExists doesUserExist(@PathParam("user") String username);
}
The other way of doing this would be to register providers that know how to convert strings and booleans into XML, but that's messy and affects your whole application in unexpected ways and you really shouldn't do that for simple types, OK?
Upvotes: 2