Skip to main content

First CXF-RS service with Tomcat web-container

Looking to get started on CXF-RS, well that was what i was looking for and there are load of articles doing however none of them provide sufficient bullet points to make me understand how the control is following are how to debug the application if something is not working. Well that's what i am going to write here.

Following are components you need to get started with first CXF-RS:
  1. Tomcat
  2. Eclipse (or some equivalent IDE)
  3. Spring
Create a dynamic web project in the eclipse (this is nothing but a simple web project which can deployed and can run when deployed in the web container like Tomcat).

Keep your eye on the bold terms featuring in the files...

Start will writing the web.xml descriptor file.






contextConfigLocation
WEB-INF/deploy-context.xml



org.springframework.web.context.ContextLoaderListener




CXFServlet
CXF Servlet

org.apache.cxf.transport.servlet.CXFServlet

1


CXFServlet
/*






Second step is writing the deploy-context.xml and keep it in WEB-INF folder.








Java classes needed for the this:



1. com.test.User.java
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "user")
public class User {


    private Integer id;


    private String  name;


    public User() {
    }


    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }


    public Integer getId() {
        return id;
    }


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


    public void setName(String name) {
        this.name = name;
    }


    public String getName() {
        return name;
    }


    @Override
    public String toString() {
        return String.format("{id=%s,name=%s}", id, name);
    }
}


----------------------------------------------------------------------


2. com.test.UserCollection.java



import java.util.Collection;


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class UserCollection {


    private Collection users;


    public UserCollection() {
    }


    public UserCollection(Collection users) {
        this.users = users;
    }


    @XmlElement(name="user")
    @XmlElementWrapper(name="users")
    public Collection getUsers() {
        return users;
    }
}

------------------------------------------------------------------------------

3. com.test.IService.java

import javax.ws.rs.core.Response;

public interface IService {

    UserCollection getUsers();

    User getCustomer(Integer id);

    Response getBadRequest();

}

-----------------------------------------------------------------------------

4. com.test.ServiceImpl 


import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

@Path("/myservice/")
@Produces("application/json")
public class ServiceImpl implements IService {

private static Map users = new HashMap();

static {
users.put(1, new User(1, "foo"));
users.put(2, new User(2, "bar"));
users.put(3, new User(3, "baz"));
}

public ServiceImpl() {
System.out.println("hello");
}

@GET
@Path("/users")
public UserCollection getUsers() {
return new UserCollection(users.values());
}

@GET
@Path("/user/{id}")
public User getCustomer(@PathParam("id") Integer id) {
return users.get(id);
}

@GET
@Path("/users/bad")
public Response getBadRequest() {
return Response.status(Status.BAD_REQUEST).build();
}
}

That's it you are done. Your app structure would look similar to this:

App
--WEB-INF
----lib
----classes
----web.xml
----deploy-context.xml


Deploy this app in the tomcat server and try hitting following urs:





Comments

Popular posts from this blog

Interview at Progress Software

I have given a lot of interviews before however, this one was special. Let me start from the beginning. I got a call from Progress Software and was scheduled for the first telephonic discussion. Next day, I got a call from HR and she asked me questions on Java and a few basic puzzles.  After clearing the previous round, I got scheduled for the second telephonic interview. This interview covered Java, design-related problems and algorithms. After a week, I got a call from the HR for the onsite interviews in Hyderabad. This is the first time I googled about Progress Software. I said yes for the onsite interviews. Journey to onsite started not as planned after my flight got delayed. Because of fog-related delay in Delhi, I reached the PSI (Progress Software India) office couple of hours late.  HR introduced me to the first interviewer. The interviewer explained the first round. It was a coding round (I am not a big fan of coding rounds), the problem was ...

java.net.SocketException: Connection reset

I am able to fix the problem via setting the following params on the HTTPClient class... client.getParams().setParameter("http.socket.timeout", new Integer(0)); client.getParams().setParameter("http.connection.stalecheck", new Boolean(true)); java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77) at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105) at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115) at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832) at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590) at org.apache.commons.httpclient.HttpMethodBase.execute(...

CXF-RS: Java Code Generation from xml/xsd

Cleaner approach is to use xjc like this: Continue to dirty approach below if u need to: When working with CXF-RS services often we start with XML message that will be served. After that we go through endless iteration of generating the right Java POJO's for the XML. The library like apache XMLBeans generate the Java code but the code generated is not what a developer would write and generally tedious to modify and understand. I have come across a work around for generating Java classes from XSD (xml) which is very close to what a developer with decent experience will write.  Follow the steps given below to explore the solution: 1. Generate the wsdl from from xsd as given in at this link  ( http://cxf.apache.org/docs/xsd-to-wsdl.html ). 2. The Java classes can be generated from wsdl from maven plugin entry below find complete details here   http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html org.apache.cxf cxf-codegen-plugin ...