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
Related
I have a endpoint like this
#PostMapping(value = "/create")
public Mono<?> issueToken(#RequestBody IssuePayTokenRequest request) {
return Mono.fromCallable(() -> tokenManagementService.issuePayToken(request)).subscribeOn(Schedulers.boundedElastic());
}
And the IssuePayTokenRequest is a protobuf message, like this
message IssuePayTokenRequest {
string client_id = 1;
}
According to Google protobuf's style guide, I should use snake case style for fields, so when client call this endpoint, the request body should like this.
{ "client_id": "abcdefg"}
However, for JSON style guide, the field should use camel case, like this
{ "clientId": "abcdefg"}
Can I do some configuration to let Spring Boot auto convert a camel case style request body to a snake case style protobuf message?
PS: It's legacy project and I'm not familiar with Spring, but I found this code in the Configuartion class.
#Bean
public ObjectMapper objectMapper() {
return new Jackson2ObjectMapperBuilder()
.featuresToDisable(
JsonGenerator.Feature.IGNORE_UNKNOWN,
MapperFeature.DEFAULT_VIEW_INCLUSION,
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
)
.serializationInclusion(JsonInclude.Include.ALWAYS)
.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.modulesToInstall(ProtobufModule.class)
.build();
}
#Override
protected void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
ObjectMapper objectMapper = objectMapper();
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper));
configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper));
}
Seems it should convert camel case json to snake case protobuf automaticly. But it doesn't work as I wish.
this can be achieved by adding the following property
spring.jackson.property-naming-strategy=SNAKE_CASE
Try put this configuration on your DTO:
#JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
Or, you can do a custom configuration to objectMapper bean to affect all objects:
#Bean
ObjectMapper objectMapper() {
return new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
In my application I configured Jackson to use SerializationFeature.WRAP_ROOT_VALUE and DeserializationFeature.UNWRAP_ROOT_VALUE globally.
#Configuration
public class AppConfig {
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE, DeserializationFeature.UNWRAP_ROOT_VALUE);
return builder;
}
}
This configuration works fine but now I am in a situation where in deserialization case I get a JSON Response without rootname. So I have got a Service Class which builds a RestTemplate using RestTemplateBuilder and POST some Data to a REST-Webservice.
#Service
public class ApiServiceImpl
implements ApiService<RegisterResponse> {
private RestTemplate restTemplate;
public ApiServiceImpl(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder
.errorHandler(new RestTemplateResponseErrorHandler()).build();
}
#Override
public ResponseEntity<RegisterResponse> callAPI(String requestAsJson,
String username, String password) {
ResponseEntity<RegisterResponse> result = null;
HttpHeaders headers = getHeaders(username, password);
result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<String>(requestAsJson, headers),
RegisterResponse.class);
return result;
}
}
The Response looks like the following:
{
"redirect-url": "https://any-url.com/?with=params"
}
And I want to deserialize this to the following POJO directly. (Like in restTemplate.exchange configured)
public class RegisterResponse {
#JsonProperty("redirect-url")
private String redirectUrl;
//getter/setter
}
It's clear to get this exception because of the UNWRAP_ROOT_VALUE Feature:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'redirect-url' does not match expected ('RegisterResponse') for type [simple type, class xxx.xxx.xxxservice.xxx.model.response.entity.RegisterResponse]
at [Source: (String)"{
"redirect-url": "https://any-url.com/?with=params"
}"; line: 2, column: 5]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1356)
at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:4087)
How can I configure Jackson to dont use DeserializationFeature.UNWRAP_ROOT_VALUE in this particular case?
Like JB Nizet commented, its possibly by setting a new Instance of MappingJackson2HttpMessageConverter and ObjectMapper for Jakson to the list of MessageConverters.
restTemplate.getMessageConverters().add(getCustomConverter());
private MappingJackson2HttpMessageConverter getCustomConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MappingJackson2HttpMessageConverter customConverter =
new MappingJackson2HttpMessageConverter(mapper);
if (!restTemplate.getMessageConverters()
.removeIf(MappingJackson2HttpMessageConverter.class::isInstance)) {
new RuntimeException("Custom MappingJackson2HttpMessageConverter not found");
}
return customConverter;
}
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 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.
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());
}
}