无法向 Spring 中的@RequestBody 发送 POST 请求,错误 415
Can not Send POST request to @RequestBody in Spring, error 415
我目前正在做一个项目,我需要向 spring 发送 POST 请求。我已经寻找或已经寻找了几个小时的解决方案,但没有找到可行的解决方案。当我开发该部分时,该请求有效。问题是,在创建了一些新功能(另一个控制器中的 2 个新端点)之后,POST 创建或更新实体的请求停止工作而没有更改特定区域中的代码。
控制者:
@RestController
@CrossOrigin
@RequestMapping
public class SensorController {
@PostMapping(value = "/createSensor", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UUID> insertSensor(@RequestBody SensorDto sensorDto){
UUID sensorId = sensorService.createSesor(sensorDto);
return new ResponseEntity<>(sensorId,HttpStatus.CREATED);
}
}
consumes 和 produces 的部分原来是没有的,我试了一下,因为在其他帖子上看到了,但没有帮助这种情况。
SensorDto:
public class SensorDto extends RepresentationModel<SensorDto> {
private UUID id;
private String description;
private Integer maxValue;
private Device device;
来自POSTMAN的电话:
image
headers:headers
有人可以帮我让它重新工作吗?
编辑:从另一个控制器询问的代码
@PostMapping("/addSensorToDevice")
public ResponseEntity<UUID> addSensor(@RequestBody DeviceSensorLinkDto deviceSensorLinkDto){
System.out.println("OOO: " + deviceSensorLinkDto.toString());
if(deviceService.addSensor(deviceSensorLinkDto)){
return new ResponseEntity<>(deviceSensorLinkDto.getDeviceId(), HttpStatus.OK);
}else {
return new ResponseEntity<>(deviceSensorLinkDto.getDeviceId(), HttpStatus.EXPECTATION_FAILED);
}
}
@PostMapping("/addClientToDevice")
public ResponseEntity<UUID> addClient(@RequestBody DeviceClientLinkDto deviceClientLinkDto){
System.out.println("OOO: " + deviceClientLinkDto.toString());
if(deviceService.addClient(deviceClientLinkDto)){
return new ResponseEntity<>(deviceClientLinkDto.getDeviceId(), HttpStatus.OK);
}else {
return new ResponseEntity<>(deviceClientLinkDto.getDeviceId(), HttpStatus.EXPECTATION_FAILED);
}
}
这个有效,删除传感器实体的请求也有效。
您的应用程序中似乎有多个 @JsonBackReference
和 @JsonManagedReference
,因此,您必须为所有对提供一个名称,如下所示:
@JsonBackReference(value = "something")
@JsonManagedReference(value = "something")
@JsonBackReference(value = "something-else")
@JsonManagedReference(value = "something-else")
您可以在 reference documentation:
中找到一些相关信息
Logical have for the reference property pair; used to link managed and
back references. Default name can be used if there is just single
reference pair (for example, node class that just has parent/child
linkage, consisting of one managed reference and matching back
reference).
我目前正在做一个项目,我需要向 spring 发送 POST 请求。我已经寻找或已经寻找了几个小时的解决方案,但没有找到可行的解决方案。当我开发该部分时,该请求有效。问题是,在创建了一些新功能(另一个控制器中的 2 个新端点)之后,POST 创建或更新实体的请求停止工作而没有更改特定区域中的代码。
控制者:
@RestController
@CrossOrigin
@RequestMapping
public class SensorController {
@PostMapping(value = "/createSensor", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UUID> insertSensor(@RequestBody SensorDto sensorDto){
UUID sensorId = sensorService.createSesor(sensorDto);
return new ResponseEntity<>(sensorId,HttpStatus.CREATED);
}
}
consumes 和 produces 的部分原来是没有的,我试了一下,因为在其他帖子上看到了,但没有帮助这种情况。
SensorDto:
public class SensorDto extends RepresentationModel<SensorDto> {
private UUID id;
private String description;
private Integer maxValue;
private Device device;
来自POSTMAN的电话: image
headers:headers
有人可以帮我让它重新工作吗?
编辑:从另一个控制器询问的代码
@PostMapping("/addSensorToDevice")
public ResponseEntity<UUID> addSensor(@RequestBody DeviceSensorLinkDto deviceSensorLinkDto){
System.out.println("OOO: " + deviceSensorLinkDto.toString());
if(deviceService.addSensor(deviceSensorLinkDto)){
return new ResponseEntity<>(deviceSensorLinkDto.getDeviceId(), HttpStatus.OK);
}else {
return new ResponseEntity<>(deviceSensorLinkDto.getDeviceId(), HttpStatus.EXPECTATION_FAILED);
}
}
@PostMapping("/addClientToDevice")
public ResponseEntity<UUID> addClient(@RequestBody DeviceClientLinkDto deviceClientLinkDto){
System.out.println("OOO: " + deviceClientLinkDto.toString());
if(deviceService.addClient(deviceClientLinkDto)){
return new ResponseEntity<>(deviceClientLinkDto.getDeviceId(), HttpStatus.OK);
}else {
return new ResponseEntity<>(deviceClientLinkDto.getDeviceId(), HttpStatus.EXPECTATION_FAILED);
}
}
这个有效,删除传感器实体的请求也有效。
您的应用程序中似乎有多个 @JsonBackReference
和 @JsonManagedReference
,因此,您必须为所有对提供一个名称,如下所示:
@JsonBackReference(value = "something")
@JsonManagedReference(value = "something")
@JsonBackReference(value = "something-else")
@JsonManagedReference(value = "something-else")
您可以在 reference documentation:
中找到一些相关信息Logical have for the reference property pair; used to link managed and back references. Default name can be used if there is just single reference pair (for example, node class that just has parent/child linkage, consisting of one managed reference and matching back reference).