Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part IV (JAX-RS’ BeanParam Example)

This post will explain JAX-RS’ yet another awesome feature, the BeanParam.

Part I – A simple RESTful Webservices using Apache CXF
Part II – Testing using RestTemplate and JUNIT
Part III – Exception/Error handling using ExceptionMapper
Part IV – BeanParam Example

BeanParam can be used if you have to inject multiple parameters (@PathParam, @FormParam etc.,) to a resource method. Instead by using BeanParam annotation, the framework will inject a custom aggregated value object.

It’s also useful if the service has to accept parameters from various sources such as Form, Header, Body, Query etc.,

Aggregated POJO (BeanParamModel.java)


package com.idodevjobs.sample.model;

import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam;

public class BeanParamModel {
    @FormParam("myData")
    private String data;

    @HeaderParam("myHeader")
    private String header;

    @PathParam("id")
    private String id;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "BeanParamModel{" +
                "data='" + data + '\'' +
                ", header='" + header + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}


Service Definition Interface (ExampleService.java)


@POST
@Path("/beanparam/{id}")
public ExampleModel postBeanParam(@BeanParam BeanParamModel beanParamModel);

Service Implementation (ExampleServiceImpl.java)


@Override
public ExampleModel postBeanParam(BeanParamModel beanParamModel) {
    return new ExampleModel(beanParamModel.toString(), 1001);
}

Refer to the first post in the series for the other required classes and files.

In our example, the BeanParamModel POJO has 3 parameters –

  • data – from FormParam “myData”
  • header – from HeaderParam “myHeader”
  • id – from PathParam “id”

Instead of injecting these individual parameters to the postBeanParam – resource method (in ExampleService), we are just injecting the aggregated BeanParamModel using the annotation BeanParam.

The war has to be built and deployed on tomcat/jetty for testing. This can be tested using any REST client. The below screenshot how it’s tested on postman – rest client.

testing beanparam - rest services on postman rest client

Please share if you find this useful. Please comment for any questions/issues.

Advertisements
This entry was posted in java and tagged , , , , . Bookmark the permalink.

6 Responses to Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part IV (JAX-RS’ BeanParam Example)

  1. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part III (Exception/Error handling using ExceptionMapper) | iDoDevJobs

  2. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part II (Testing using RestTemplate and JUNIT) | iDoDevJobs

  3. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part I | iDoDevJobs

  4. Anonymous says:

    very nice. thanks

    Like

  5. PAINTER MATHAVAN says:

    Very good example

    Like

  6. Thanks a lot ! Great read

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s