我有一個通用類
@NoArgsConstructor
public class CustomData<T> {
}
下面是它使用的類。
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Recipient {
@NotEmpty(message = "Please provide email details of recipient")
public String email;
public CustomData<?> custom_data;
}
以下是我嘗試使用的有效負載
"recipients": [
{
"custom_data": {},
"email": "string"
}
]
但是我收到一條錯誤訊息 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class
有人可以幫忙嗎?感謝您的時間
uj5u.com熱心網友回復:
Please add the property SerializationFeature.FAIL_ON_EMPTY_BEANS= false to your object mapper like objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//or you can add this code to your any class of springboot which have @Configuration annotation on it.
@Bean
public ObjectMapper getMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return objectMapper;
}
Send Payload as
{
"custom_data": {},
"email": "emailvalue"
}
//Now I am supposing your api is like this
@PostMapping(value = "/show/recipient") /* /preview-email-recipient*/
public ResponseEntity<?> showRecipient(@Valid @RequestBody Recipient recipient){
log.info(String.format("recipient received {%s} ",recipient.toString()));
return new ResponseEntity<>(recipient,HttpStatus.OK);
}
//curl call will be
endpoint will be post call : http://localhost:8080/show/recipient
with requestbody as : {
"customData": {},
"email": "emailvalue"
}
response : {
"customData": {},
"email": "emailvalue"
}
Reason for failure was ?
0
when you return your object i.e. (Recipient in this case) in response it is getting Serialized to json string using ObjectMapper which is used in spring's MessageConverter(i.e Jackson2HttpMessageConverter) bean. Now the error is caused due to how ObjectMapper serializes your class. Your class has 2 field, 1 of type String and 1 of type JSONObject/GenericType. ObjectMapper when serializing fields, tries to find the corresponding serializer based on the field type. There are some out-of-the-box implementation of serializer for known type like String but for your custom type you either need to provide serializer to ObjectMapper bean or have to disable serialization via configuration of set property SerializationFeature.FAIL_ON_EMPTY_BEANS to false.
Now what does SerializationFeature.FAIL_ON_EMPTY_BEANS do??
public static final SerializationFeature FAIL_ON_EMPTY_BEANS
Feature that determines what happens when no accessors are found for a type (and there are no annotations to indicate it is meant to be serialized). If enabled (default), an exception is thrown to indicate these as non-serializable types; if disabled, they are serialized as empty Objects, i.e. without any properties.
Note that empty types that this feature has only effect on those "empty" beans that do not have any recognized annotations (like @JsonSerialize): ones that do have annotations do not result in an exception being thrown.
Feature is enabled by default.
So
1 way was to disable serialization on empty beans.
2nd way you can annotate CustomData class with @JsonSerialize i.e. you
are provinding the mapper which serializer you have to used for this
param.
so Make CustomData class as---
@NoArgsConstructor
@JsonSerialize
public class CustomData<T> {
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427912.html
