0

次のWebサービス呼び出しがあります

@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

デフォルトでは、Spring は JSON マーシャル/アンマーシャルに Jackson を使用しようとします。

    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 に答える