Providing GORM classes from Spring Download

I am trying to write a simple Spring boot controller that displays a GORM instance and does not work.

Here's a shortened version of my code:

@RestController @RequestMapping("/user") class UserController { @RequestMapping(value='/test', method=GET) User test() { return new User(username: 'my test username') } } 

The following error message appears:

Could not write JSON: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: users.domain.User["errors"]->grails.validation.ValidationErrors["messageCodesResolver"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: users.domain.User["errors"]->grails.validation.ValidationErrors["messageCodesResolver"])

It seems that the error is caused by additional properties introduced by GORM. What is the proposed solution for this? Will this be finally resolved in gorm-hibernate4-spring-boot ? Should I just turn off SerializationFeature.FAIL_ON_EMPTY_BEANS (I don't have much experience with Jackson, so I'm not quite sure what side effects this might have)? Should I use Jackson's annotations to solve the problem? Any other options?

+6
source share
3 answers

I found a way to get rid of the error using this code:

 @Component class ObjectMapperConfiguration implements InitializingBean { @Autowired ObjectMapper objectMapper @Override void afterPropertiesSet() { def validationErrorsModule = new SimpleModule() validationErrorsModule.addSerializer(ValidationErrors, new ErrorsSerializer()) objectMapper.registerModule(validationErrorsModule) } } class ErrorsSerializer extends JsonSerializer<ValidationErrors> { @Override void serialize(ValidationErrors errors, JsonGenerator jgen, SerializerProvider provider) { jgen.writeStartObject() jgen.writeEndObject() } } 

Obviously, this solution is far from perfect, as it simply destroys all validation errors, but now it is good enough for me. I am sure that the Spring boot command will have to solve this problem, as GORM objects are also serialized with some internal Hibernate properties such as attached . I do not accept this answer as this is not an acceptable solution for most scenarios, it basically just suppresses the exception.

+1
source

This did not work for me. So I used this instead, and the error went away.

 @JsonIgnoreProperties(["errors"]) 
0
source

I am using springBootVersion '1.4.1.RELEASE' with gorm and hibernate5:

 compile("org.grails:gorm-hibernate5-spring-boot:6.0.3.RELEASE") 

I need to include the following at the beginning of each domain class in order to use them in the client response (e.g. json serialization using jackson):

  @JsonIgnoreProperties(["errors", "metaClass", "dirty", "attached", "dirtyPropertyNames"]) 

When using springBootVersion '1.3.5.RELEASE' I managed to avoid:

 @JsonIgnoreProperties(["errors"]) 

This is a trend in the wrong direction :)

0
source

Source: https://habr.com/ru/post/971771/


All Articles