user6577633 user6577633 - 3 months ago 15
reST (reStructuredText) Question

how to write rest service using @beanparam and @Get mthod

Please help me to write rest webservice using below class and @beanparam and @Get method

@QueryParam("prop1")
public String prop1;

@QueryParam("prop2")
public String prop2;

@QueryParam("prop3")
public String prop3;

@QueryParam("prop4")
public String prop4;

Answer

With a POJO like this:

public class MyBean {

    @QueryParam("prop1") 
    private String prop1;

    @QueryParam("prop2") 
    private String prop2;

    @QueryParam("prop3") 
    private String prop3;

    @QueryParam("prop4") 
    private String prop4;

    // Getters and setters omitted
}

Your resource method would be like:

@GET
@Path("/foo")
public Response foo(@BeanParam MyBean myBean) {
    ...
}

Update: As mentioned in the comments, to return the MyBean marshaled into XML in the HTTP response payload, you would have something as following:

@GET
@Path("/foo")
@Produces(MediaType.APPLICATION_XML)
public Response foo(@BeanParam MyBean myBean) {
    return Response.ok(myBean).build();
}

Ensure you have a XML provider in the dependecies. Otherwise, you will have an error like:

javax.ws.rs.ProcessingException: could not find writer for content-type application/xml
Comments