Great, that seems good :) Glad you finally succeeded!
Le jeudi 6 octobre 2016 15:54:53 UTC+2, [email protected] a écrit : > > I got it working. Here's what I did. > > I had to implement a custom serialization to XML. Reason why is because I > my model is based on EMF <http://www.eclipse.org/modeling/emf/> and it > does its own XML. But I also had to support JSON. I did an implementation > of MessageBodyWriter for my XML serialization. I did nothing for my JSON > serialization. > > The key was to register my MessageBodyWriter with jersey. I did it in in > the run method of my Application like this: > > @Override > public void run(ForecastConfiguration configuration, Environment > environment) > throws Exception { > environment.jersey().register( > new IndexResource()); > > environment.jersey().getResourceConfig().register(new > FHIRMessageWriter<Conformance>()); > } > > > My MessageBodyWriter looks like this: > > @Produces(MediaType.APPLICATION_XML) > public class FHIRMessageWriter<T extends EObject> implements > MessageBodyWriter<T> { > > Logger log = LoggerFactory.getLogger(FHIRMessageWriter.class); > > protected static ResourceSet resourceSet = new ResourceSetImpl(); > protected static Resource resource; > > static { > FhirPackage.eINSTANCE.eClass(); > XhtmlPackage.eINSTANCE.eClass(); > resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap > ().put("xml", new XMLResourceFactoryImpl()); > resource = resourceSet.createResource(URI.createURI("xxx.xml")); > } > > @Override > public long getSize(T arg0, Class<?> arg1, Type arg2, Annotation[] > arg3, MediaType arg4) { > return 0; > } > > @Override > public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2 > , MediaType arg3) { > return true; > } > > @Override > public void writeTo(T ir, Class<?> clazz, Type type, Annotation[] ann, > MediaType mt, > MultivaluedMap<String, Object> mvm, java.io.OutputStream > stream) > throws IOException, WebApplicationException { > log.trace("writeTo==>"); > resource.getContents().add((EObject) ir); > > try { > resource.save(stream, Collections.EMPTY_MAP); > } catch (IOException e) { > log.error("", e); > } > > } > } > > One of my REST calls in IndexResource looks like this: > > @GET > @Path("/Conformance") > @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) > public Conformance getConformance(@HeaderParam("content-type") String > contentType) { > Conformance conformance = FhirFactory.eINSTANCE.createConformance > (); > conformance.setId(FHIRUtil.createId()); > conformance.setUrl(ForecastUtil.URIs.FORECAST_CONFORMANCE.uri); > conformance.setStatus(FHIRUtil.CONFORMANCE_STATUS.DRAFT.code); > conformance.setExperimental(FHIRUtil.BOOLEAN.TRUE.bool); > conformance.setFhirVersion(FHIRUtil.createId()); > conformance.setDate(FHIRUtil.convertDateTime(new Date())); > conformance.getProfile().add(ForecastUtil.PROFILEs. > FORECAST_IMMUNIZATIONRECOMMENDATIONRECOMMENDATION.reference); > conformance.getProfile().add(ForecastUtil.PROFILEs. > FORECAST_IMMUNIZATIONRECOMMENDATION.reference); > conformance.getProfile().add(ForecastUtil.PROFILEs. > FORECAST_IMMUNIZATION.reference); > conformance.getProfile().add(ForecastUtil.PROFILEs. > FORECAST_PATIENT.reference); > conformance.getFormat().add(FHIRUtil.FORMAT.JSON.code); > conformance.getFormat().add(FHIRUtil.FORMAT.XML.code); > return conformance; > } > > > Finally, I tested it all with these using RestAssured: They dump the > results so I can do a visual. > > @Test > public void testConformanceJSON() { > given(). > accept(ContentType.JSON). > when(). > get("/forecast/Conformance").then().log().body(); > } > > @Test > public void testConformanceXML() { > given(). > accept(ContentType.XML). > when(). > get("/forecast/Conformance").then(). > log().body(); > } > > > > On Monday, October 3, 2016 at 8:55:27 AM UTC-4, > [email protected] wrote: >> >> All, >> >> Using DW 0.9.0. >> >> I need to have jersey serialize my model into either JSON or XML depending >> on the request. I think I get that part. >> But my model is generated using EMF (Eclipse Modeling Framework) and so >> annotating code would be, to say the least, ill advised. I need to implement >> custom serialization for XML. As I understand it, that means implementing >> the interface InjectableProvider, which should be a part of jersey, however >> I am not finding it >> in DW. How can I get this done the DW way? >> >> Thanks in advance >> >> -- You received this message because you are subscribed to the Google Groups "dropwizard-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
