我尝试在我的服务的字段中测试验证,但是当我将响应消息放入邮递员中时,不显示(消息和状态)
我在Stackoverflow中搜索了很多内容,没有找到我的案子
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
@NotNull
private String clientName;
@Column(name = "date_of_birth", nullable = false)
@Temporal(TemporalType.DATE)
/** @JsonFormat(pattern="dd/MM/yyyy") **/
private Date dateOfBirth;
@Column(nullable = false)
@NotNull
private String mobileNumber;
@Column(nullable = false)
@NotNull
@Email(message = "Email should be valid")
private String email;
@Column(nullable = false)
@NotNull
private String address;
@Column(nullable = false)
@NotNull
private String sex;
@NotNull(message = "weight cannot be null")
private Integer weight;
@NotNull(message = "hight cannot be null")
private Integer hight;
@Column(nullable = false)
@NotNull
private String healthNote;
@Column(nullable = false)
@NotNull
private String importantNote;
@Column(nullable = false)
@NotNull
private String personToContact;
@Column(nullable = false)
@NotNull
private String relation;
@Column(nullable = false)
@NotNull
private String phoneNumber;
@PostMapping("/uploadProfileClient")
public ResponseEntity<?> uploadMultipartFile(@Valid @RequestPart("addClient") String clientNew ,@Valid @RequestPart(value = "image") MultipartFile image,BindingResult result) throws JsonParseException, JsonMappingException, IOException {
clientEntity client = null;
Map<String,Object> response = new HashMap<>();
if(result.hasErrors()) {
List<String> errors = result.getFieldErrors().stream().map(err -> "The field '" + err.getField() +"' "+ err.getDefaultMessage()) .collect(Collectors.toList());
response.put("Errors",errors);
return new ResponseEntity<Map<String,Object>>(response, HttpStatus.BAD_REQUEST);
}
ObjectMapper mapper = new ObjectMapper();
client = mapper.readValue(clientNew, clientEntity.class);
client.setImage(image.getBytes());
try {
clientService.save(client);
} catch ( DataAccessException e) {
response.put("message", "Error when inserting into the database");
response.put("error", e.getMessage().concat(": ").concat(e.getMostSpecificCause().getMessage()));
return new ResponseEntity<Map<String,Object>>(response,HttpStatus.INTERNAL_SERVER_ERROR);
}
response.put("message", "the client data has been created successfully!");
response.put("client", client);
return new ResponseEntity<Map<String,Object>>(response,HttpStatus.CREATED);
}
我将以json和文件的形式发送数据,响应在邮递员中未显示,请回答。
答案 0 :(得分:1)
问题很直接,Weight
属性接受Integer
,但是您正在发送"weight":"as"
,这就是为什么您得到Deserialize
纠正它的原因。
尝试下面的虚拟数据
{
"clientName":"foo",
"dateOfBirth":"2020-03-19",
"mobileNumber":"9911",
"email":"asd@email.com",
"address":"sa",
"sex":"m",
"weight":"1",
"hight":"12",
"healthNote":"note",
"importantNote":"imp",
"personToContact":"myself",
"relation":"single",
"phoneNumber":"mynumber"
}
同样,您也不必使用string
手动将Entity
转换为ObjectMapper
。 Spring可以解决这个问题,因此可以更改控制器
@PostMapping("/uploadProfileClient")
public ResponseEntity<?> uploadMultipartFile(@Valid @RequestPart("addClient") ClientEntity clientNew ,@Valid @RequestPart(value = "image") MultipartFile image,BindingResult result) throws JsonParseException, JsonMappingException, IOException {
//now you can save clientEntity directly
client.setImage(image.getBytes());
clientService.save(client);
//your logic
}
如何向PostMan
请求