package sample.jersey.providers; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.emfjson.jackson.module.EMFModule; import javax.inject.Inject; 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.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @Provider @Produces("application/json") public class JsonResourceBodyWriter implements MessageBodyWriter<Resource> { @Inject ResourceSet resourceSet; @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return type == Resource.class || Resource.class.isAssignableFrom(type); } @Override public long getSize(Resource t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; } @Override public void writeTo(Resource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new EMFModule(resourceSet)); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.writeValue(entityStream, resource); } }