2016-07-25 50 views
2

我使用Java Spring Cloud/Boot构建了一个REST API服务。首先,我做了一个简单的类连接到一个MongoDB和一个控制器,该控制器应该允许我添加,删除,更新和获取所有对象。当使用POSTMAN这些都可以工作,但是当我想使用redux和fetch API添加或更新一个对象时,我得到一个状态400和“错误的请求”错误。这似乎与我在正文中发送的JSON有关,但它与例如POSTMAN一起使用的JSON格式完全相同。Java Spring REST API状态400对POST/PUT的响应

我在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 
     })) 
    } 
} 

控制器代码的Java春:

@RequestMapping(method = RequestMethod.POST, path = "/add") 
public void addAsset(@RequestBody Asset asset) { 
    assetService.addAsset(asset); 
} 

状态OK,在做的在邮递员:

postman request

我使用终极版/提取API时得到的错误(我只删除目录结构,因为它有公司名称):

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

可以复制/粘贴您的资产Java对象? –

+0

添加了代码。 – Matthias

+0

你错过了你的json中的ID,因此得到了400.你能尝试告诉我结果吗? –

回答

2

您的错误信息说:

;需要请求主体缺少

我认为当你controller方法 试图形成从传入请求的对象发生错误。

当你发送请求时你必须要set每个字段与对象有关。 如果您打算未设置属性,则应使用@JsonIgnore注释标记该字段。

您可以在变量上使用@JsonIgnore注释,在形成对象时以及在输出对象时将忽略此属性 。

使用@JsonIgnore注解放在setter method,我认为你应该做的,因为现在 发出请求时,你忽略了id property

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