Gre*_*rin 6 groovy grails-orm spring-boot
我正在尝试编写一个简单的Spring Boot控制器来渲染GORM实例并失败.
这是我的代码的缩短版本:
@RestController
@RequestMapping("/user")
class UserController {
@RequestMapping(value='/test', method=GET)
User test() {
return new User(username: 'my test username')
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
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"])
该错误似乎是由GORM注入的额外属性引起的.建议的解决方案是什么?这最终会解决gorm-hibernate4-spring-boot吗?我应该简单地禁用SerializationFeature.FAIL_ON_EMPTY_BEANS(我没有很多杰克逊的经验,所以我不完全确定这可能有什么副作用)?我应该使用杰克逊的注释来解决问题吗?还有其他选择吗?
我找到了一种使用以下代码消除错误的方法:
@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()
}
}
Run Code Online (Sandbox Code Playgroud)
显然这个解决方案远非完美,因为它只是消除了所有验证错误,但现在它对我来说已经足够好了。我非常确定 Spring Boot 团队最终必须解决这个问题,因为 GORM 对象也正在使用一些内部 Hibernate 属性(例如attached. 我不接受这个答案,因为对于大多数情况来说,它不是可接受的解决方案,它基本上只是压制异常。
| 归档时间: |
|
| 查看次数: |
1024 次 |
| 最近记录: |