【问题标题】:Error on Spring REST webservice callSpring REST Web 服务调用出错
【发布时间】:2013-05-13 18:35:19
【问题描述】:

我有以下网络服务调用

@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 网络服务。

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

【问题讨论】:

  • 发生这种情况是因为 Spring 无法解组您提供给 DirectoryUser 实例的 json。您可以发布该课程的代码吗?以及您传递的 json 的示例。
  • 请在上面查看我发布的 DirectoryUser 代码和 json 字符串。

标签: web-services spring rest


【解决方案1】:
                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。

【讨论】:

  • 问题是请求何时发送到服务器。而不是它返回的内容和处理方式。
  • 这是简单的 Http 请求。不是 HTTPS。
  • 在 web.xml 中是 /*
  • 问题不在于 url 模式。我有 /api/* 作为 url 模式。
【解决方案2】:

默认情况下,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 库添加到您的项目中。

【讨论】:

  • 我添加了 Jackson 解析器和 JSON 转换器。现在我可以正常访问上述网络服务了。
猜你喜欢
  • 2015-04-29
  • 2014-12-14
  • 1970-01-01
  • 2015-08-04
  • 2012-07-13
  • 2017-03-19
  • 2017-02-18
相关资源
最近更新 更多