Spring REST Web服务调用出错

时间:2013-05-13 18:35:19

标签: web-services spring rest

我有以下网络服务电话

@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和使用简单路径变量的情况下访问其他webservice调用。问题出在@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”}

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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库添加到您的项目中。