我有一个支持用户帐户的Spring 4 Web应用程序。它有一个用于创建帐户和登录的Web前端。现在我尝试使用REST Web服务创建新用户。我能够get
和delete
用户使用网络服务,但当我尝试创建新用户或更新现有用户时,我收到http 400错误 - 错误请求。以下是我的代码。
创建用户的服务方法:
@RequestMapping(value="/createuser", method=RequestMethod.POST, headers="Accept=application/json", produces="application/html")
public ResponseEntity<String> createUser(@RequestBody User user){
logger.debug("insdie put");
logger.debug(user);
boolean result = userService.createUserServices(user);
if(result){
return new ResponseEntity<>(HttpStatus.CREATED);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
网址&amp;数据:
http://localhost:8080/XXX/createuser
{
username: "arun.talwar1@yahoo.com",
enabled: 1
password: "arun"
authority: {
username: "arun.talwar1@yahoo.com"
authorityType: "candidate"
},
userDetails: {
username: "arun.talwar1@yahoo.com"
firstName: "Arun1"
lastName: "Talwar"
}
}
请求&amp;响应:
POST /DummyJobPortal/services/createuser HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 233
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1011
Date: Mon, 09 Nov 2015 17:26:12 GMT
Connection: close
用户类:
@Entity
@Table(name = "users")
@XmlRootElement
public class User {
@Id
@Column(name = "username")
private String username;
@Column(name = "enabled")
private int enabled;
@Column(name = "password")
private String password;
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "username")
private Authority authority;
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "username")
private UserDetails userDetails;
@XmlElement(name="userDetails")
public UserDetails getUserDetails() {
return userDetails;
}
public void setUserDetails(UserDetails userDetails) {
this.userDetails = userDetails;
}
@XmlElement(name="authority")
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
@XmlElement(name="username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@XmlElement(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement(name="enabled")
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
}
我已经通过互联网寻找答案,但它指出格式错误的请求,服务器无法理解请求。但这与我使用http get
方法请求用户时获得的格式相同。任何建议,最受欢迎。
答案 0 :(得分:0)
您的JSON格式不正确会阻止服务器处理您的请求。您的属性需要以逗号分隔,并且需要引用属性名称。
以下是JSON
的格式正确的版本{
"username": "arun.talwar1@yahoo.com",
"enabled": 1,
"password": "arun",
"authority": {
"username": "arun.talwar1@yahoo.com",
"authorityType": "candidate"
},
"userDetails": {
"username": "arun.talwar1@yahoo.com",
"firstName": "Arun1",
"lastName": "Talwar"
}
}
大概400-BadRequest响应是由于服务器无法解析您的JSON请求。