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.
Related
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
I'm trying to parse the JSON from Github's commit details API into a HashMap. The Sample Json I've used in my tests is here
The test code is
#SpringJUnitConfig(classes = {GithubApiService.class, RestTemplateConfiguration.class})
#EnableAutoConfiguration
public class GithubApiServiceTest {
#Test
public void testGithubResponseJsonToMapConversion(
#Autowired RestTemplate restTemplate,
#Autowired GithubApiService service,
#Value("classpath:github/commit-payload.json") Resource commitPayloadFile) throws IOException {
final String COMMITS_URL = "https://api.github.com/repos/Codertocat/Hello-World/commits/sha";
//Stub response from Github Server
String responseJson = new ObjectMapper().writeValueAsString(
new String(Files.readAllBytes(Paths.get(commitPayloadFile.getFile().getAbsolutePath()))));
MockRestServiceServer mockGithubServer = MockRestServiceServer.createServer(restTemplate);
mockGithubServer.expect(requestTo(COMMITS_URL))
.andRespond(withSuccess().contentType(APPLICATION_JSON).body(responseJson));
//Call API Service
Map<String, Object> result = service.getCommitDetails(COMMITS_URL);
//Expect return type is hashmap
assertThat(result.get("sha")).isEqualTo("6dcb09b5b57875f334f61aebed695e2e4193db5e");
}
}
The Service code is
#Service
#AllArgsConstructor(onConstructor = #__(#Autowired))
public class GithubApiService {
#Autowired
private final RestTemplate restTemplate;
public Map<String, Object> getCommitDetails(String commitsUrl) {
ParameterizedTypeReference<Map<String, Object>> responseType = new ParameterizedTypeReference<Map<String, Object>>() {
};
RequestEntity<Void> request = RequestEntity.get(URI.create(commitsUrl)).accept(APPLICATION_JSON).build();
return restTemplate.exchange(request, responseType).getBody();
}
}
This fails converting the JSON response into a Map with the following error (full log here)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==",
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
Since I'm using spring-boot-starter-web it auto-wires converters including MappingJackson2HttpMessageConverter but debugger shows that the response is being processed by the ByteArrayHttpMessageConverter despite setting content type to application/json.
My bad, I should have used the #RestClientTest annotation intead of #EnableAutoConfiguration, that fixed the problem.
#EnableAutoConfiguration is a more generic annotation which doesn't achieve all the auto config that #RestClientTest does
I want to receive a Map<Timestamp, Integer> as PathVariable using Spring MVC. Is this possible?
This is my controller:
#ApiOperation(value = "Some Api", produces = MediaType.APPLICATION_JSON_VALUE)
#PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<RestResult> createApi(#RequestPart (value="image", required = false) MultipartFile banner, SomeRequest request){
RestResult result = new RestResult();
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
This is my request:
#Getter
#Setter
public class SomeRequest implements Serializable {
#NonNull
private MultiValueMap<Timestamp, Integer> someValue;
}
As one of the way you could use Spring's HandlerMethodArgumentResolver and implement its supportsParameter() and resolveArgument() methods. The last one accepts the method parameter and web request, which you can use to get any required data and compose the object whatever you want.
The argument resolver is invoked before the controller method and makes the required argument transformation.
Update:
You have to register the resolver to make it active. If you use spring-boot, you just need to define it as a bean.
Hope it will help.
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
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());
}
}