1. Error handle class
@ControllerAdvice
public class ErrorHandler {
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorResponse errorResponse(Exception exception) {
return exception.getMessage();
}
}
then we can got clear error message
Reference
http://www.importnew.com/7903.html
2. Change response's content-type
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
need for just return JSON string
Reference
3. Customize MappingJackson2HttpMessageConverter
's behaviors
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
// configure ObjectMapper here
//builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.featuresToEnable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS);
return builder;
}
add the above code snippet to any of your Configuration classes
Reference
4. Let Spring @RestController
gives Avro objects a hug
make Avro generated class objects automatically converted to JSON string by
MappingJackson2HttpMessageConverter
Two methods:
avro_object.toStirng()
and change response's content-type to application/json- configure
MapperFeature.REQUIRE_SETTERS_FOR_GETTERS
totrue
for Jackson'sObjectMapper
, use the method at above section.
References
https://github.com/FasterXML/jackson-dataformat-avro/issues/16
Comments