我的控制器有一个后端服务器:
@RestController
@RequestMapping("/rsrv")
public class ReservationController {
@Autowired
private ReservationService service;
@Autowired
private ReservationMapper mapper;
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, value = "createReservation")
public void createReservation(@RequestBody ReservationDto reservationDto) {
service.saveReservation(mapper.mapToReservation(reservationDto));
}
// + other methods
}
以及通过 HTTP 请求与后端通信的前端。这是一些配置:
@Component
public class ApiClient {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiClient.class);
@Autowired
private RestTemplate restTemplate;
@Value("${api.endpoint}")
private String baseEndpoint;
private URI createReservationURI(LocalDateTime date, Long patientId, Long doctorId) {
return UriComponentsBuilder.fromHttpUrl(baseEndpoint + "/rsrv/createReservation")
.queryParam("id", getReservations().size() + 1000)
.queryParam("time", LocalDateTime.now())
.queryParam("patientId", getReservations().size() + 1001)
.queryParam("doctorId", getReservations().size() + 1002)
.build().encode().toUri();
}
public void createReservation(ReservationDto reservationDto) {
try {
restTemplate.postForObject(createReservationURI(reservationDto.getTime(),
reservationDto.getPatientId(),
reservationDto.getDoctorId()),
null,
CreatedReservationDto.class);
System.out.println("Reservation added!");
} catch (RestClientException e) {
LOGGER.error(e.getMessage(), e);
System.out.println("Reservation hasn't been added!");
}
}
}
我试图解决这个问题,同时我创建了一个单独的类(它几乎是原始类的克隆,实际上在上面的 postForObject 方法中使用):
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreatedReservationDto {
@JsonProperty("id")
private Long id;
@JsonProperty("time")
private LocalDateTime time;
@JsonProperty("patientId")
private Long patientId;
@JsonProperty("doctorId")
private Long doctorId;
public CreatedReservationDto(Long id, LocalDateTime time, Long patientId, Long doctorId) {
this.id = id;
this.time = time;
this.patientId = patientId;
this.doctorId = doctorId;
}
public CreatedReservationDto() {
}
// + getters and setters
createReservation 方法仍然不起作用,我已经搜索了答案,似乎请求本身没有错,但服务器端不仅接受 JSON 数据,但我不知道该怎么做。有什么帮助吗?