javarestresteasy

How can I pass a custom type as parameter to RestEasy service?


I feel embarrassed to ask: but what is the right combination of annotations for a resteasy service method that will unmarshall a custom type?

I am able to successfully generate json and xml from methods which return custom types (with jaxb annotations), but I have failed to turn these types into method parameters. All the examples around the web seem to pass simple types such as strings.

Documentation claims that resteasy can unmarshall json and xml to annotated types, but how? The following signature requires an object with a string parameter taking constructor, which is not what I'm looking for.

@GET
@Path("/somepath/ontheserver/settestchild")
@Produces("application/xml")
String getQueryParam(@QueryParam("testchild")TestChild param);

TestChild has JAXB annotations, but I want resteasy to unmarshal incoming xml to an instance of this object, which is not happening. Am I missing something here?


Solution

  • You can use the @Consumes annotation:

    @PUT
    @Path("/")
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    TestChild addTestChild(TestChild testChild);