Issue
I am working currently on a project and I need to send a POST request to spring. I looked or several hours already for a solution and didn't find one to work. The request worked when I developed that part. The problem is that after creating some new functionalities(2 new endpoint in another controller) the POST requests for creating or updating the entities stopped working without changing code in the specific area.
The Controller:
@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);
}
}
The part with consumes and produces wasn't there originally, I tried it because saw on other posts but doesn't helped the situation.
The SensorDto:
public class SensorDto extends RepresentationModel<SensorDto> {
private UUID id;
private String description;
private Integer maxValue;
private Device device;
The call from POSTMAN: image
The headers: headers
Can someone help me to get it to work again?
EDIT: The code asked from the other controller
@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);
}
}
And this one works and also the requests for deleting a Sensor entity.
Solution
It seems that you have multiple @JsonBackReference
and @JsonManagedReference
in your application and as a consequence, you must provide a name for all pairs as follows:
@JsonBackReference(value = "something")
@JsonManagedReference(value = "something")
@JsonBackReference(value = "something-else")
@JsonManagedReference(value = "something-else")
You can find some information about this in the 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).
Answered By - João Dias