import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
/**
* This class will make sure JSON timestamps is written in the format yyyy-MM-dd'T'hh:mm:ss'Z'
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonConfig() {
objectMapper = new ObjectMapper();
objectMapper.configure(
SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
Showing posts with label RESTEasy. Show all posts
Showing posts with label RESTEasy. Show all posts
Sunday, March 9, 2014
RESTEasy format timestamps as date
By default RESTEasy using Jackson will format a java.util.Date as a unix-timestamp. To change it into ISO_8601 formar, yyyy-MM-dd'T'hh:mm:ss'Z', simply create a class
Monday, January 27, 2014
RESTEasy services with older application servers
If you are not blessed with the latest application server and want to use all the fancy new features offered by RESTEasy, fear not! You can still use RESTEasy framework with older application servers like JBoss 4.x.
Add the 4 dependencies below. If you are using maven, here is a shortcut:
Loading the RESTEasy framework is very easy. Simple configure it in web.xml.
To make spring available in the rest services, add another listener in the web.xml.
And now a simple JSON REST service, to check everything works.
A simple POJO class:
And a service who offers two methods. One who consumes and produces a JSON POST request and another who produces JSON.
Accessing the GET method, http://localhost:8080/war-file/rest/v1/get-service/2 will output:
Accessing the POST, http://localhost:8080/war-file/rest/v1/post-service method with payload:
Will output:
That is all it takes to get started with RESTEasy using an older application server, happy REST/SOAP service development :).
Add the 4 dependencies below. If you are using maven, here is a shortcut:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.6.Final</version>
</dependency>
Loading the RESTEasy framework is very easy. Simple configure it in web.xml.
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<!-- Set servlet-mapping for the Resteasy servlet if it has a url-pattern other than /* -->
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
To make spring available in the rest services, add another listener in the web.xml.
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
And now a simple JSON REST service, to check everything works.
A simple POJO class:
public class RestObject {
private long id;
private String name;
private String value;
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
And a service who offers two methods. One who consumes and produces a JSON POST request and another who produces JSON.
@Path("/v1")
public class RestService {
@POST
@Path("/post-service")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addRestObject(RestObject restObject) {
restObject.setId(123L);
return Response.status(HttpResponseCodes.SC_OK).entity(restObject).build();
}
@GET
@Path("/get-service/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getRestObject(@PathParam("id") long id) {
RestObject restObject = new RestObject();
restObject.setId(id);
restObject.setName("your name");
restObject.setValue("JP");
return Response.status(HttpResponseCodes.SC_OK).entity(restObject).build();
}
}
Accessing the GET method, http://localhost:8080/war-file/rest/v1/get-service/2 will output:
{
"id": 2,
"name": "your name",
"value": "JP"
}
Accessing the POST, http://localhost:8080/war-file/rest/v1/post-service method with payload:
{
"id": 1,
"name": "my name",
"value": "JP"
}
Will output:
{
"id": 123,
"name": "my name",
"value": "JP"
}
That is all it takes to get started with RESTEasy using an older application server, happy REST/SOAP service development :).
Subscribe to:
Posts (Atom)