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

Master Detail

Master-Detail Eclipse Forms I just finished working on the editor which uses the Master-Detail pattern of the eclipse forms. During which I faced several problems like: In my master block, when the size of table-viewer increased, the master block shows the scrollbar instead of table-viewer. Also, it made the length of the detail and master parts equal which made my editor unusable Detail part having scroll problems like the one mentioned above Setting the details part data-dynamically was yet another area where I faced a lot of problem In this blog, I will be giving a brief introduction about the master-detail pattern and will be addressing the problems mentioned above. The article by Dejan Glozic is really helpful and explains the basics of the eclipse forms. I’ll be explaining the stuff using FUSE Integration Designer(FID) which is an open source and available for download . And the screen-shots will be from the Messaging module of the FID using the example of JMS Messages . In

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 well expl

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(