Я настроил запрос POST в моем приложении для реакции следующим образом:
await fetch('http://localhost:8080/api/room', { credentials: 'include' }, {
'method': 'POST',
'headers': {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials': true
},
'body': JSON.stringify(somedata),
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log('Error', error);
});
В моем приложении для весенней загрузки у меня есть контроллер:
@RequestMapping(value = "/room", produces = "application/json", method = {RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<Room> createRoom(@Valid @RequestBody Room room) throws URISyntaxException {
Room result = roomService.saveOrUpdate(room);
return ResponseEntity.created(new URI("/api/room/" + result.getRoomClientId()))
.body(result);
}
В консоли браузера яполучить этот ответ:
Response {type: "cors", url: "http://localhost:8080/api/room",
redirected: false, status: 400, ok: false, …}
В консоли весенней загрузки я получаю эту ошибку:
2019-04-08 16:31:42.742 WARN 86177 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<com.sbpoc.app.db.entity.Room> com.sbpoc.app.db.controller.RoomController.createRoom(com.sbpoc.app.db.entity.Room) throws java.net.URISyntaxException]
Ошибка говорит, что тело запроса отсутствует?
Обновление
import uniqid from 'uniqid';
const someData = {
roomNumber: uniqid(),
}
Номер комнаты:
@Entity
public class Room implements Serializable {
private static final long serialVersionUID = 3433823138738252949L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String roomNumber;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(String roomNumber) {
this.roomNumber = roomNumber;
}
}