Skip to content

Instantly share code, notes, and snippets.

@codler
Forked from amolbrid/AppError.java
Created May 24, 2013 09:17
Show Gist options
  • Save codler/5642335 to your computer and use it in GitHub Desktop.
Save codler/5642335 to your computer and use it in GitHub Desktop.
package com.ab.jersey.dto;
public class AppError {
private String message;
public AppError() { }
public AppError(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.ab.jersey.dto;
import javax.ws.rs.core.Response.Status;
public class AppResponse {
private Status status = Status.OK;
private Object body;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Object getBody() {
return body;
}
public void setBody(Object body) {
this.body = body;
}
}
package com.ab.jersey;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
public class AppServer {
public static final Logger logger = Logger.getLogger(AppServer.class);
public static void main(String[] args) {
try {
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages", "com.ab.jersey.services");
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create("http://localhost:9090/", initParams);
System.in.read();
threadSelector.stopEndpoint();
} catch(Throwable t) {
t.printStackTrace();
logger.error(t.getMessage(), t);
}
}
}
package com.ab.jersey.services;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import com.ab.jersey.dto.AppResponse;
import com.ab.jersey.dto.Message;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class HelloService {
@GET
public Message index() {
Message msg = new Message("Hello");
return msg;
}
@GET
@Path("/list")
public List<Message> messageList() {
List<Message> messages = new ArrayList<Message>();
messages.add(new Message("Hello"));
messages.add(new Message("Amol"));
return messages;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Message create(Message message) {
AppResponse ar = new AppResponse();
ar.setStatus(Status.OK);
ar.setBody(message);
return message;
}
}
package com.ab.jersey.services;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.JsonParseException;
import com.ab.jersey.dto.AppError;
@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {
@Override
public Response toResponse(JsonParseException ex) {
AppError error = new AppError("Invalid json.");
return Response.ok(error).build();
}
}
package com.ab.jersey.dto;
import org.codehaus.jackson.annotate.JsonProperty;
public class Message {
private String message;
private String status;
public Message() { }
public Message(String msg) {
this.message = msg;
this.status="Ok";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@JsonProperty(value="st")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
package com.ab.jersey.services;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.ObjectMapper;
import com.ab.jersey.dto.AppResponse;
import com.ab.jersey.dto.AppError;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class MessageBodyWriterJSON implements MessageBodyWriter<Object> {
@Override
public long getSize(Object obj, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return true;
}
@Override
public void writeTo(Object target, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream os)
throws IOException, WebApplicationException {
ObjectMapper om = new ObjectMapper();
AppResponse ar = new AppResponse();
if(target instanceof Exception ) {
ar.setStatus(Status.BAD_REQUEST);
ar.setError(((Exception)target).getMessage());
}
else if(target instanceof AppError) {
ar.setBody(target);
ar.setStatus(Status.BAD_REQUEST);
} else {
ar.setBody(target);
}
om.writeValue(os, ar);
}
}
Required Jar file:
asm-3.1.jar jersey-core-1.9.1.jar
commons-logging-api-1.1.1.jar jersey-grizzly-1.9.1.jar
grizzly-servlet-webserver-1.9.18-i.jar jersey-json-1.9.1.jar
jackson-core-asl-1.8.3.jar jersey-server-1.9.1.jar
jackson-jaxrs-1.8.3.jar jsr311-api-1.1.1.jar
jackson-mapper-asl-1.8.3.jar log4j-1.2.15.jar
jackson-xc-1.8.3.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment