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

java.net.SocketException: Connection reset

Hide empty last column from Table/Tree Viewer

Spring: implements interface not working in @Controller