我有以下网络服务调用
@RequestMapping(value = "modifyUser/{userDn}", method = RequestMethod.POST, headers="Accept=application/json")
public @ResponseBody
JSONObject modifyUser(@PathVariable String userDn, @RequestBody DirectoryUser directoryUser) {
// Modify User
boolean modifiedUser = this.authenticationService.modifyUser(userDn, directoryUser.getPassword(), directoryUser);
// Build JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("modifyUser", modifiedUser);
return jsonObject;
}
我正在使用以下客户端方法访问以上 REST Web 服务。
String url = "http://www.local.com:8080/CommonAuth-1.0-SNAPSHOT/api/authentication/modifyUser/";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url + "user6.external")
JSONObject ob = new JSONObject();
ob.put("description", "updated");
System.out.println(ob.toString());
StringEntity entity = new StringEntity(ob.toString());
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
我总是收到“服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持的格式”错误。我的代码有什么问题。我可以在不使用 @RequestBody 和使用简单路径变量的情况下访问其他 Web 服务调用。问题在于@RequestBody 以及我如何使用 HttpPost。
public class DirectoryUser {
private String displayName;
private String fullName;
private String userName;
private String firstName;
private String lastName;
private String description;
private String country;
private String company;
private String phone;
private String emailAddress;
private String password;
private boolean expirePassword = true;
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isExpirePassword() {
return expirePassword;
}
public void setExpirePassword(boolean expirePassword) {
this.expirePassword = expirePassword;
}
}
我发布的 JSON 字符串是 {"description":"updated"}