Last active
August 8, 2022 11:54
-
-
Save sprelacart/9758892 to your computer and use it in GitHub Desktop.
Inject ObjectMapper instance via @context into REST method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tomtom.dcs.springconfig; | |
| import java.util.Arrays; | |
| import javax.ws.rs.core.Application; | |
| import javax.ws.rs.ext.RuntimeDelegate; | |
| import org.apache.cxf.bus.spring.SpringBus; | |
| import org.apache.cxf.endpoint.Server; | |
| import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.annotation.DependsOn; | |
| import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; | |
| import com.fasterxml.jackson.jaxrs.xml.JacksonXMLProvider; | |
| import com.tomtom.dcs.service.AccountsService; | |
| import com.tomtom.dcs.service.GreetingsService; | |
| import com.tomtom.dcs.service.PackagesService; | |
| import com.tomtom.dcs.service.PackageListService; | |
| @Configuration | |
| public class CxfConfig | |
| { | |
| @Autowired | |
| GreetingsService greetingsService; | |
| @Autowired | |
| PackageListService packageListService; | |
| @Autowired | |
| AccountsService accountsService; | |
| @Autowired | |
| PackagesService packagesService; | |
| @Bean(destroyMethod = "shutdown") | |
| public SpringBus cxf() | |
| { | |
| return new SpringBus(); | |
| } | |
| @Bean | |
| @DependsOn("cxf") | |
| public Server jaxRsServer() | |
| { | |
| JAXRSServerFactoryBean factory = | |
| RuntimeDelegate.getInstance().createEndpoint(new Application(), JAXRSServerFactoryBean.class); | |
| factory.setServiceBeans(Arrays.<Object>asList( | |
| greetingsService, | |
| packageListService, | |
| accountsService, | |
| packagesService)); | |
| factory.setAddress("/api" + factory.getAddress()); | |
| factory.setProviders(Arrays.<Object>asList( | |
| new JacksonJsonProvider(), | |
| new JacksonXMLProvider(), | |
| new ObjectMapperResolver())); | |
| return factory.create(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tomtom.dcs.springconfig; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.core.Context; | |
| import javax.ws.rs.core.MediaType; | |
| import javax.ws.rs.ext.ContextResolver; | |
| import javax.ws.rs.ext.Provider; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; | |
| /** | |
| * A JAX-RS {@link Provider} used to provide an Jackson {@link ObjectMapper} to | |
| * all root resource class. Is automatically used by {@link JacksonJsonProvider} | |
| * and can be accessed by every method via {@link Context @Context} method parameter | |
| * injection for custom usage. | |
| * | |
| * @author grundleo | |
| */ | |
| @Provider | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public class ObjectMapperResolver implements ContextResolver<ObjectMapper> | |
| { | |
| private ObjectMapper objectMapper; | |
| public ObjectMapperResolver() | |
| { | |
| super(); | |
| objectMapper = new ObjectMapper(); | |
| } | |
| @Override | |
| public ObjectMapper getContext(Class<?> type) | |
| { | |
| return objectMapper; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @POST | |
| @Consumes(MediaType.APPLICATION_JSON) | |
| @Produces(MediaType.APPLICATION_JSON) | |
| @Path("packagelists/{id}") | |
| @Override | |
| public DcsPackageList updatePackageList(String dcsPackageList, | |
| @PathParam("id") String packageListId, | |
| @Context ContextResolver<ObjectMapper> objectMapperResolver) | |
| { | |
| // access the commonly used ObjectMapper: some JAX-RS magic to use always the same ObjectMapper instance | |
| ObjectMapper om = objectMapperResolver.getContext(DcsPackageList.class); | |
| // read the object to be updated | |
| DcsPackageList dcsPackageListToUpdate = packageListDao.findById(Long.valueOf(packageListId)); | |
| // do something custom with the mapper | |
| // do a partial update: update the loaded object with the incoming values | |
| om.readerForUpdating(dcsPackageListToUpdate).readValue(dcsPackageList); | |
| // update it in the database | |
| packageListDao.update(dcsPackageListToUpdate); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment