0

我有以下网络服务调用

@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"}

4

2 回答 2

0
                try {
                /* your code */
if (httpResponse != null) {
                            InputStream in = httpResponse.getEntity().getContent();
                            StringBuffer out = new StringBuffer();
                            int n = 1;
                            while (n > 0) {
                                byte[] b = new byte[4096];
                                n = in.read(b);
                                if (n > 0)
                                    out.append(new String(b, 0, n));
                            }
                            System.out.println(out.toString());
                        }
                    } catch (Exception e) {
                    e.printStackTrace();
                }

你能检查一下状态码说什么吗?另外,请检查它是否是HTTPS。

于 2013-05-13T18:42:37.520 回答
0

默认情况下,Spring 尝试将 Jackson 用于 json marshall/unmarshall:

    private static final boolean jackson2Present =
        ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", WebMvcConfigurationSupport.class.getClassLoader()) &&
                ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader());

您需要将 Jackson 库添加到您的项目中。

于 2013-05-14T11:05:51.080 回答