I have swagger2 with springfox configured on a Spring MVC app exposing rest services. One of the exposed services has a map as Requestbody parameter:
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST, produces= "application/json", headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> uploadFilesToGED(@RequestParam(value = "userId") String userId, @RequestBody Map<String,DocumentMetaData> documents ) {
GEDService.pushDocument(userId, documents);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
The problem is that swagger-ui doesn't show me a blank model for the map attribute but only a pair of empty brackets {} . I would have expected the model to show me a default map representation with a String and the model of the object DocumentMetaData (which contains himself other Maps and Lists...):
So after googling a while I found some posts talking about ObjectMapper configuration. Mine is very simple:
public class RestAPIConfig extends WebMvcConfigurerAdapter {
...
/**
* configureMessageConverters
*/
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(jackson2Converter());
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new ResourceHttpMessageConverter());
converters.add(new SourceHttpMessageConverter<Source>());
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(new ObjectHttpMessageConverter());
}
/**
* MappingJackson2HttpMessageConverter
*
* @return Converter from java to json
*/
@Bean
public MappingJackson2HttpMessageConverter jackson2Converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
/**
* Object mapper
*
* @return objectMapper
*/
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
return objectMapper;
}
...
}
Why does I see empty brackets instead of a blank representation of the map?
Thx