Adding msgpack as content negotiation to spring mvc + boot - java

I'm trying to add msgpack binary dataformat as a content negotiation option. The Json and Xml works fine out of the box. I tried to add jackson msgpack mapper as a bean, like in this examle, but it does not work. When I add Accept: application/x-msgpack header to my request 406 Not Acceptable code is returned.
Here's my WebConfig:
#Configuration
#EnableWebMvc
#SuppressWarnings("unused")
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public HttpMessageConverter messagePackMessageConverter() {
return new AbstractJackson2HttpMessageConverter(
new ObjectMapper(new MessagePackFactory()),
new MediaType("application", "x-msgpack")) {
};
}
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false)
.ignoreAcceptHeader(false)
.favorParameter(true)
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("msgpack", new MediaType("application", "x-msgpack"));
}
}
I didn't add any special annotations to my DTO objects, and my controller is nothing out of the ordinary either.
My msgpack dependency is:
org.msgpack:jackson-dataformat-msgpack:0.7.0-p3

Apparently bean injection did not work (I would be glad if someone showed me how to auto inject a new HttpMessageConverter). So I added it manually:
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(messagePackMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
converters.add(new MappingJackson2XmlHttpMessageConverter());
super.configureMessageConverters(converters);
}
//...

Can be done in 2 ways -
With spring boot just creating a #Bean of type HttpMessageConverter should do the job. spring boot will add it automatically to the list of message converters. (But doing this also means if the existing application was returning "application/json" as default response when Accept header was not supplied then it will now return default "application/x-msgpack")
#Bean
public HttpMessageConverter messagePackMessageConverter() {
MediaType msgPackMediaType = new MediaType("application", "x-msgpack");
return new AbstractJackson2HttpMessageConverter(new ObjectMapper(new
MessagePackFactory()), msgPackMediaType) {
};
}
this solution is similar to the above solution of using WebMvcConfigurerAdapter but instead of using configureMessageConverters I would suggest using extendMessageConverters , since using configureMessageConverters would turn off default converter registration done by spring. Usage example -
#Configuration
class WebConfig implements WebMvcConfigurer {
HttpMessageConverter messagePackMessageConverter() {
MediaType msgPackMediaType = new MediaType("application", "x-msgpack");
return new AbstractJackson2HttpMessageConverter( new ObjectMapper( new
MessagePackFactory()), msgPackMediaType) {
};
}
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters)
{
converters.add(messagePackMessageConverter());
}
}

Related

Understand and configure Spring JSON marshalling configuration of String responses

For a Spring MVC project (not Spring Boot) I'm configuring the JSON converter to customise JSON responses of all REST endpoints i.e. removing null fields and setting a date format. After introducing SpringDoc to the project I've had to add a StringHttpMessageConverter to prevent the generated OpenAPI JSON from being returned as a string.
Without the StringHttpMessageConverter the OpenAPI JSON looks like this:
"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"http://localhost:8080\",\"description\":\"Generated server url\"}],\"paths\":{\"/get\":{\"get\":{\"tags\":[\"controller\"],\"operationId\":\"getSomeMap\",\"responses\":{\"200\":{\"description\":\"default response\",\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/components/schemas/ImmutableMultimapStringString\"}}}}}}}},\"components\":{\"schemas\":{\"ImmutableMultimapStringString\":{\"type\":\"object\",\"properties\":{\"empty\":{\"type\":\"boolean\"}}}}}}"
With the StringHttpMessageConverter it looks like this, which is the desired result:
{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8080","description":"Generated server url"}],"paths":{"/get":{"get":{"tags":["controller"],"operationId":"getSomeMap","responses":{"200":{"description":"default response","content":{"*/*":{"schema":{"$ref":"#/components/schemas/ImmutableMultimapStringString"}}}}}}}},"components":{"schemas":{"ImmutableMultimapStringString":{"type":"object","properties":{"empty":{"type":"boolean"}}}}}}
This does however cause problems with several endpoints that return a string as their response. They should return a valid JSON string: "response-string" but instead they return the string as plain text: response-string, omitting the double quotes, making it invalid JSON.
How can I keep the current configuration intact so the SpringDoc OpenAPI JSON is returned correctly while also having endpoints that have a string response return a valid JSON string?
Configuration used:
#Configuration
#EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
#Override
public void addInterceptors(InterceptorRegistry registry) {
WebContentInterceptor webContentInterceptor = new WebContentInterceptor();
webContentInterceptor.setCacheSeconds(0);
webContentInterceptor.setUseExpiresHeader(true);
webContentInterceptor.setUseCacheControlHeader(true);
webContentInterceptor.setUseCacheControlNoStore(true);
registry.addInterceptor(webContentInterceptor);
}
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
Map<String, MediaType> mediaTypes = new HashMap<>();
mediaTypes.put("json", MediaType.APPLICATION_JSON);
mediaTypes.put("xml", MediaType.APPLICATION_XML);
configurer.favorPathExtension(false);
configurer.favorParameter(true);
configurer.mediaTypes(mediaTypes);
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// Note that the order matters here! If the StringHttpMessageConverter is add after the jsonConverter
// the documentation JSON is returned as a giant string instead of a (valid) JSON object
converters.add(new StringHttpMessageConverter());
converters.add(jsonConverter());
}
#Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.timeZone(TimeZone.getTimeZone(timeZone));
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(
builder.build()
);
jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
return jsonConverter;
}
#Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setClassesToBeBound(KioskProfiel.class, KioskProfielRegel.class, TitlesetTO.class, TitlesetTitel.class);
return jaxb2Marshaller;
}
#Bean
public MarshallingHttpMessageConverter marshallingConverter() {
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_XML);
MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter(jaxb2Marshaller());
marshallingConverter.setSupportedMediaTypes(supportedMediaTypes);
return marshallingConverter;
}
}
Edit
I've tried overriding the OpenApiResource setting the produces value of the endpoint to TEXT_PLAIN_VALUE and too application/json but the problem still persists. Attempting to change the return type from String to TextNode isn't allowed so that doesn't seem to be an option.
Alternatively I've tried to resolve the problem by registering a Filter to correct the malformed response but that to doesn't work.
Maybe I'm still missing something but I'm out of options. With my current project configuration I can't get SpringDoc to return valid OpenAPI JSON when using a custom MappingJackson2HttpMessageConverter. For now I'll stick to Swagger 2.0 and will look into an alternative library to move to OpenAPI 3.0.
A working solution has finally been found! It consists of two parts. The first is configuring the converters. In short we register the default converts after which the default JSON converter, MappingJackson2HttpMessageConverter is removed and our custom JSON converter is added as the first converter to the list of converters. It is important that the custom JSON converter is in the list of converters before the StringHttpMessageConverter else endpoints that return JSON that have a String as their Java return type return the string without double quotes making it invalid JSON.
#Configuration
#EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
converters.add(0, jsonConverter());
}
#Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.timeZone(TimeZone.getTimeZone(timeZone));
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(
builder.build()
);
jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
return jsonConverter;
}
}
Secondly, this causes the OpenAPI JSON to be one big (escaped) string, as mentioned in the question. To resolve this problem we override the openapiJson method (and endpoint) from the OpenApiWebMvcResource class, which is used by default to return the OpenAPI JSON, to produce text/plain instead of application/json. This way the documentation JSON isn't returned as an (escaped) string anymore.
#RestController
public class OpenApiResource extends OpenApiWebMvcResource {
#Override
#Operation(hidden = true)
#GetMapping(value = Constants.API_DOCS_URL, produces = MediaType.TEXT_PLAIN_VALUE)
public String openapiJson(
HttpServletRequest request,
#Value(Constants.API_DOCS_URL) String apiDocsUrl
)
throws JsonProcessingException {
calculateServerUrl(request, apiDocsUrl);
OpenAPI openAPI = this.getOpenApi();
return Json.mapper().writeValueAsString(openAPI);
}
}
Note that for brevity only the relevant methods are listed in both example classes above.
Another quick workaround is to configure swagger-ui to use the YAML version of the OpenAPI documentation (instead of JSON) by simply adding this property to your application.yaml:
springdoc.swagger-ui.url: /v3/api-docs.yaml

Spring Controller #RequestMapping does not support for non English special characters

I am currently working on an old spring app which exposes REST API for mobile devices. I need to create a new REST service for a Json request with multiple language support. But when I testing this it only support English. When I try with an other language it gives me ????? characters for relevant data.
following is my configuration class
#Configuration
#EnableScheduling
#ComponentScan(basePackages = "lk.test.com.controller")
public class ControllerConfig extends WebMvcConfigurationSupport {
private static final Logger log = LoggerFactory.getLogger(ControllerConfig.class);
#Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
converters.add(getMappingJackson2HttpMessageConverter());
addDefaultHttpMessageConverters(converters);
}
#Bean
public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypes);
return converter;
}
#Bean
public ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}
}
Following is a sample controller method
#RequestMapping(value = "send/push", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
#ResponseBody
public Response sendPush(final #RequestBody CustomerExternalRequest request, HttpServletRequest servletRequest) {
System.out.println(request.getPushMessage());
Response response = new Response();
return response;
}
and Relevant Request Object
#Getter
#Setter
#ToString
public class CustomerExternalRequest extends Request{
private String phoneNo;
//push send
private Boolean isOnlyToCurrenltyLoggedDevice;
private String pushMessage;
private String pushTitle;
}
my Json Request
{"request":{"phoneNo":"+94776587745","isOnlyToCurrenltyLoggedDevice":true,"pushMessage":"测试","pushTitle":"测试"}}
And this is my POSTMAN request
System.out.println(request);
prints ?? in the console
Can any one know how to solve it? Thanks.
Finally found the reason and the solution.
When I tried with a spring boot sample app it worked. Because it is has an embedded tomcat server. In my case I used eclipse jetty plugin. I then found a place to change encoding type and changed it to UTF-8 and now it works fine.
See the configuration screen shot here

Dropwizard custom ExceptionMapper for javax.validation.ConstraintViolationException

I'm using Dropwizard and would like to create a custom ExceptionMapper to handle javax.validation.ConstraintViolationException. I've created a customer mapper:
#Provider
public class MyExceptionMapper implements ExceptionMapper<Exception> {
public Response toResponse(Exception exception) {
return Response.status(500)
.entity(exception.getMessage())
.type(MediaType.TEXT_PLAIN)
.build();
}
}
and configured it in my Dropwizard app:
#Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
ServerFactory serverFactory = configuration.getServerFactory();
if (serverFactory instanceof AbstractServerFactory) {
((AbstractServerFactory) serverFactory).setRegisterDefaultExceptionMappers(false);
}
environment.jersey().register(new MyExceptionMapper());
}
When a resource/controller throws a javax.validation.ConstraintViolationException exception it's not handled by my custom mapper, instead it goes through org.glassfish.jersey.server.validation.internal.ValidationExceptionMapper which returns a 400 which is not what I want.
Is it possible to override/remove Jersey's mapper? I would have thought setting setRegisterDefaultExceptionMappers(false); would do the trick but it seems that it doesn't.

Using different jackson ObjectMappers for seperate RequestMapping

I have a single #RequestMapping that consumes a custom MIME type. The request uses an ObjectMapper bean defined in the #Configuration to enabled the JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.
This feature allows typically invalid json (treating backslashes as a non-special character) to be consumed, which is a requirement of this particular #RequestMapping to allow google encoded polylines to be parsed directly. However this means that this ObjectMapper is now being used for ALL of my #RequestMapping when it is really only a requirement for one.
Is there a way to differentiate the ObjectMapper being used for each #Controller or #RequestMapping?
Object Mapper Bean
#Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
return builder.build();
}
Request Mapping Interface Method
#ApiOperation(value = "Returns the toll cost breakdown for a journey", notes = "", response = TotalCost.class, tags={ "pricing", })
#ApiResponses(value = {
#ApiResponse(code = 200, message = "successful operation", response = TotalCost.class) })
#RequestMapping(value = "/pricing/journeyCost",
produces = { "application/json" },
consumes = { "application/vnd.toll-pricing+json" },
method = RequestMethod.POST)
ResponseEntity<TotalCost> getTollBreakdownFromEncodedPoly(#ApiParam(value = "Request object representing the journey" ,required=true ) #RequestBody Journey body);
I found the answer in another stackoverflow question linked to me by another user - https://stackoverflow.com/a/45157169/2073800
I just had to add the following #Bean to my #Configuration
#Bean
public HttpMessageConverters customConverters() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
final AbstractJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/vnd.toll-pricing+json")));
return new HttpMessageConverters(converter);
}
If you've a custom MIME type, then you can register a custom HttpMessageConverter that uses a special ObjectMapper for your MIME type, and returns false from canRead/canWrite for regular MIME types. You register your custom HttpMessageConverter like so:
#EnableWebMvc
#Configuration
#ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
messageConverters.add(myCustomMessageConverter());
super.configureMessageConverters(converters);
}
}
Think of it as related to content negotiation, and not related to URL mapping; URL mapping (#RequestMapping) is meant for finding a handler, not for choosing what marshaller/unmarshaller to use.

Spring programmatically register RequestMapping

I have a working url as this: localhost/info
#Controller
#RequestMapping("/info")
public class VersionController {
#RequestMapping(value = "", method = RequestMethod.GET)
public #ResponseBody
Map get() {
loadProperties();
Map<String, String> m = new HashMap<String, String>();
m.put("buildTimestamp", properties.getProperty("Application-Build-Timestamp"));
m.put("version", properties.getProperty("Application-Version"));
return m;
}
}
and I would to register some other mappings at initializing of my application as this:
localhost/xxxx/info
localhost/yyyy/info
localhost/zzzz/info
All these urls will return same response as localhost/info
The xxxx, yyyy part of the application is changeable. I have to register custom mappings as
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("???").setViewName("???");
}
Bu this is only working for views.
Any idea for dynamic registration?
You can register a new HandlerMapping where you can add the handlers for your URL paths; the most convenient implementation would be SimpleUrlHandlerMapping.
If you want those handlers to be bean methods (like those annotated with #RequestMapping) you should define them as HandlerMethod wrappers so that the already registered RequestMappingHandlerAdapter will invoke them.
As of Spring 5.0.M2, Spring provides a functional web framework that allows you to create "controller"-like constructs programmatically.
What you would need to do in your case is create a proper RouterFunction for the URLs you need to handle, and then simply handle the request with the appropriate HandlerFunction.
Keep in mind however that these constructs are not part of Spring MVC, but part of Spring Reactive.
Check out this blog post for more details
I believe that simple example is worth more than 1000 words :) In SpringBoot it will look like...
Define your controller (example health endpoint):
public class HealthController extends AbstractController {
#Override
protected ModelAndView handleRequestInternal(#NotNull HttpServletRequest request, #NotNull HttpServletResponse response) {
return new ModelAndView(new MappingJacksonSingleView(), "health", Health.up().build());
}
}
And create your configuration:
#Configuration
public class CustomHealthConfig {
#Bean
public HealthController healthController() {
return new HealthController();
}
#Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MAX_VALUE - 2);
mapping.setUrlMap(ImmutableMap.of("/health", healthController()));
return mapping;
}
}
Regarding the handler order - take a look about the reason here: Java configuration of SimpleUrlHandlerMapping (Spring boot)
The MappingJacksonSingleView is auxiliary class in order to return single json value for model and view:
public class MappingJacksonSingleView extends MappingJackson2JsonView {
#Override
#SuppressWarnings("unchecked")
protected Object filterModel(Map<String, Object> model) {
Object result = super.filterModel(model);
if (!(result instanceof Map)) {
return result;
}
Map map = (Map) result;
if (map.size() == 1) {
return map.values().toArray()[0];
}
return map;
}
}
Jackson single view source - this blog post: https://www.pascaldimassimo.com/2010/04/13/how-to-return-a-single-json-list-out-of-mappingjacksonjsonview/
Hope it helps!
It is possible (now) to register request mapping by RequestMappingHandlerMapping.registerMapping() method.
Example:
#Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
public void register(MyController myController) throws Exception {
RequestMappingInfo mappingInfo = RequestMappingInfo.paths("xxxx/info").methods(RequestMethod.GET).build();
Method method = myController.getClass().getMethod("info");
requestMappingHandlerMapping.registerMapping(mappingInfo, myController, method);
}
You could register your own HandlerMapping by extending the RequestMappingHandlerMapping, e.g. override the registerHandlerMethod.
It's not quite clear what you're trying to achieve, but maybe you can use #PathVariable in your #RequestMapping, something like:
#RequestMapping("/affId/{id}")
public void myMethod(#PathVariable("id") String id) {}
Edit: Original example has changed it appears, but you might be able to use PathVariable anyway.

Categories