2016-07-25 2 views
2

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); 
} 

상태 우체부에 그 일을하는 동안 :

postman request

내가 돌아 오는이/API를 가져 오기 사용할 때 얻을 오류를 (I 만 회사 이름이 있기 때문에 디렉토리 구조가 제거되었습니다.) :

request error in browser

잠시 동안 붙어있어 많은 도움이되었습니다.

편집 자산 개체 :

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; 
    } 
} 
+0

당신이 당신의 자산 자바 객체를 복사/붙여 넣기 할 수 있습니다 성공적으로 요청했다 아는 사람? –

+0

코드가 추가되었습니다. – Matthias

+0

당신은 json에 ID가 누락되어 400을 얻습니다. 결과를 알려주시겠습니까? –

답변

2

당신의 오류 메시지가 말한다 :

; 필요한 요청 본문 내가 당신 controller 방법 가 들어오는 요청에서 개체를 형성하려고 할 때 오류가 생기는 일이라고 생각

없습니다.

요청을 보낼 때 각각 객체와 관련된 모든 필드가 set이어야합니다. 속성을 설정하지 않으려면 해당 필드를 @JsonIgnore 주석으로 표시해야합니다.

이 속성을 무시할 변수에 @JsonIgnore 주석을 사용할 수 있습니다. 객체를 생성 할 때뿐만 아니라 객체를 출력 할 때도 마찬가지입니다.

사용 난 당신이 요청을 할 때 지금 때문에 당신이 id property을 무시하는해야 할 생각 setter method@JsonIgnore 주석.

@JsonIgnore 
public void setId(String id) { 
     this.id = id; 
} 

하고 controller method에서 httpstatus 코드를 반환 할 수 은 그래서 클라이언트는

@ResponseBody 
public ResponseEntity<String> addAsset(@RequestBody Asset asset) { 
return new ResponseEntity<String>("your response here", HttpStatus.OK); 
}