Java Spring Cloud/Boot를 사용하여 REST API 서비스를 만들었습니다. 첫째, MongoDB에 연결된 간단한 클래스와 서비스가있는 컨트롤러를 만들어 추가, 삭제, 업데이트 및 모든 객체 가져 오기를 허용해야합니다. POSTMAN 사용할 때 이러한 모든 작동하지만 redux 및 페치 API를 사용하여 개체를 추가 또는 업데이트 할 때 상태 400 및 "잘못된 요청"오류가 발생합니다. 이것은 내가 본문에 보내는 JSON과 관련이있는 것 같지만, 예를 들어 POSTMAN과 함께 작동하는 JSON과 정확히 동일한 형식입니다.POST/PUT에서 Java Spring REST API 상태 400 응답
Redux의 내 동작. 단순성/테스트 목적을 위해 페이지에서 전송되는 객체를 사용하는 대신 맨 위에 객체를 추가했습니다. 자바 봄
var assetObject = {
"vendor" : "why dis no work?",
"name": "wtf",
"version": "231",
"category" : "qsd",
"technology" : "whatever"
}
export function addAsset(access_token, asset) {
return dispatch => {
fetch(constants.SERVER_ADDRESS + '/as/asset/add',
{
method: 'POST',
credentials: 'include',
headers: {
'Authorization': 'Bearer' + access_token,
'Content-Type': 'application/json'
},
body: assetObject
})
.then(res => dispatch({
type: constants.ADD_ASSET,
asset
}))
}
}
컨트롤러 코드 : 확인
@RequestMapping(method = RequestMethod.POST, path = "/add")
public void addAsset(@RequestBody Asset asset) {
assetService.addAsset(asset);
}
상태 우체부에 그 일을하는 동안 :
내가 돌아 오는이/API를 가져 오기 사용할 때 얻을 오류를 (I 만 회사 이름이 있기 때문에 디렉토리 구조가 제거되었습니다.) :
잠시 동안 붙어있어 많은 도움이되었습니다.
편집 자산 개체 :
이import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "assets")
public class Asset {
@Id
private String id;
private String vendor;
private String name;
private String version;
private String category;
private String technology;
public Asset() {
}
public Asset(String id,
String vendor,
String name,
String version,
String category,
String technology) {
this.id = id;
this.vendor = vendor;
this.name = name;
this.version = version;
this.category = category;
this.technology = technology;
}
public String getId() {
return id;
}
public String getVendor() {
return vendor;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public String getCategory() {
return category;
}
public String getTechnology() {
return technology;
}
public void setId(String id) {
this.id = id;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public void setName(String name) {
this.name = name;
}
public void setVersion(String version) {
this.version = version;
}
public void setCategory(String category) {
this.category = category;
}
public void setTechnology(String technology) {
this.technology = technology;
}
}
당신이 당신의 자산 자바 객체를 복사/붙여 넣기 할 수 있습니다 성공적으로 요청했다 아는 사람? –
코드가 추가되었습니다. – Matthias
당신은 json에 ID가 누락되어 400을 얻습니다. 결과를 알려주시겠습니까? –