Java 对象 class 可以同时实现 Serializable 和 Jackson 注释吗?
Can Java object class implements Serializable and has Jackson annotation same time?
我开发了下面的 rest api 控制器并请求 DTO
DTO class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class reqDto implements Serializable{
@NotNull(message="STATUS cannot be null")
@Pattern(regexp = "^can$")
private String status;
@Pattern(regexp = "^\d{6}$|^$")
private String rgstrn;
}
控制器class:
@Path("/foo")
public class controller {
private static Logger log = Logger.getLogger(controller.class);
private static final long serialVersionUID = 1L;
@Inject
private Service Service;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/foo/bar")
public Response foo(reqDto reqDto) throws WebApplicationException {
Response resp = null;
// validate request dto bean constraints and get error message
List<String> errMsgs=Validator.validateConstraintViolation(reqDto);
RespDto resultDto = new RespDto();
// if req dto not validated, return 400
if (errMsgs.size() > 0) {
resultDto.setErrorMsg("400", BAD_REQUEST_400, errMsgs);
resp = Response.status(Integer.parseInt(resultDto.getHttpCode())).entity(resultDto).build();
} else {
// call service
resultDto = Service.createCase(reqDto);
resultDto.setErrorMsg("200", CREATED_201);
resp = Response.status(Integer.parseInt(resultDto.getHttpCode())).entity(resultDto).build();
}
return resp;
}
当我的 reqDto 实现 Serializable 时,我的过滤器出现 NullPointerException 异常 class
当我的 reqDto 没有实现 Serializable 时,出现异常
Validator - Invalid request: com.reqDto, constraint violations: [ConstraintViolationImpl{rootBean=com.reqDto@24f5f4ad, propertyPath='status', message='STATUS cannot be null', leafBean=com.reqDto@24f5f4ad, value=null}]
当我的过滤器显示负载是 { "status": "can", "rgstrn": "123456" }
在尝试了几个版本的DTO后class,我发现异常是因为getter和setter方法没有在DTO对象中创建。
Jackson 注释可以应用于 implement Serializable
的对象
我开发了下面的 rest api 控制器并请求 DTO
DTO class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class reqDto implements Serializable{
@NotNull(message="STATUS cannot be null")
@Pattern(regexp = "^can$")
private String status;
@Pattern(regexp = "^\d{6}$|^$")
private String rgstrn;
}
控制器class:
@Path("/foo")
public class controller {
private static Logger log = Logger.getLogger(controller.class);
private static final long serialVersionUID = 1L;
@Inject
private Service Service;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/foo/bar")
public Response foo(reqDto reqDto) throws WebApplicationException {
Response resp = null;
// validate request dto bean constraints and get error message
List<String> errMsgs=Validator.validateConstraintViolation(reqDto);
RespDto resultDto = new RespDto();
// if req dto not validated, return 400
if (errMsgs.size() > 0) {
resultDto.setErrorMsg("400", BAD_REQUEST_400, errMsgs);
resp = Response.status(Integer.parseInt(resultDto.getHttpCode())).entity(resultDto).build();
} else {
// call service
resultDto = Service.createCase(reqDto);
resultDto.setErrorMsg("200", CREATED_201);
resp = Response.status(Integer.parseInt(resultDto.getHttpCode())).entity(resultDto).build();
}
return resp;
}
当我的 reqDto 实现 Serializable 时,我的过滤器出现 NullPointerException 异常 class
当我的 reqDto 没有实现 Serializable 时,出现异常
Validator - Invalid request: com.reqDto, constraint violations: [ConstraintViolationImpl{rootBean=com.reqDto@24f5f4ad, propertyPath='status', message='STATUS cannot be null', leafBean=com.reqDto@24f5f4ad, value=null}]
当我的过滤器显示负载是 { "status": "can", "rgstrn": "123456" }
在尝试了几个版本的DTO后class,我发现异常是因为getter和setter方法没有在DTO对象中创建。
Jackson 注释可以应用于 implement Serializable