【问题标题】:Posting a FollowResult object returns a HTTP Error 415 Unsupported media type发布 FollowResult 对象返回 HTTP 错误 415 不支持的媒体类型
【发布时间】:2016-09-05 07:59:49
【问题描述】:

当我将追随者对象发布到我的休息电话时,我收到 415 错误,但我不明白为什么?我没有添加 dao 和其他值的私有字段,因为它的文件更大,我只将调用和客户端的代码以及 http 处理程序放在这篇文章中。

错误信息:

向 URL 发送“POST”请求:http://localhost:8080/KwetterBart-web/api/user/startfollow 响应代码:415 java.io.IOException:服务器返回 HTTP 响应代码:415 for URL:http://localhost:8080/KwetterBart-web/api/user/startfollow

这是我的课程:

接收我的休息电话的方法我认为问题出在此方法的消耗上:

@Path("/startfollow")
    @POST
    @Consumes({"application/json", "application/xml","text/xml"})
    @Produces("application/json")
    public Response startfollow(FollowResult result) {
        User user = Userdao.FindUser(result.username);
        if (user != null) 
        {
            List<User> followers;
            followers = user.getFollowers();
            User followuser = Userdao.FindUser(result.follow);
            followers.add(followuser);
            user.setFollowers(followers);
            Userdao.edit(user);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("succes", "User changed");
            return Response.ok(jsonObject.toString()).build();
        }

        JSONObject jsonObjectRequest = new JSONObject();
        jsonObjectRequest.put("error", "Cannot get a user");
        return Response.status(Response.Status.NOT_FOUND).entity(jsonObjectRequest.toString()).build();
    }

httphandler 类:

public static String sendPost(String url, String body) {
        try {
            URL oUrl = new URL(url);
            HttpURLConnection con = (HttpURLConnection) oUrl.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");

            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Content-Type", "application/json");

            System.out.println("\nSending 'POST' request to URL : " + url);

            OutputStream os = con.getOutputStream();
            os.write(body.getBytes());
            os.flush();

            int responseCode = con.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            System.out.println("Response: " + response.toString());

            in.close();

            return response.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

将代码发布到我的休息服务的客户端:

private void Startfollow(ActionEvent event)
    {
        FollowResult user = TableFollow.getSelectionModel().getSelectedItem();
        System.out.println(user.getUsername()); 

        String loginuser = TFUsername.getText();

        FollowResult follower = new FollowResult(user.getUsername(),loginuser);

        try {
            Gson gson = new Gson();
            HttpHandler.sendPost("http://localhost:8080/KwetterBart-web/api/user/startfollow",gson.toJson(follower));
            this.getfollowers(loginuser);
            } catch (Exception ex) {
                Logger.getLogger(KwetterFollowController.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

FollowResult 的对象类我在客户端和服务器端都有这个类:

public class FollowResult {

    String username;
    String follow;

    public FollowResult(String username, String follow) {
        this.username = username;
        this.follow = follow;
    }  

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFollow() {
        return follow;
    }

    public void setFollow(String follow) {
        this.follow = follow;
    }
}

【问题讨论】:

  • FollowResult 对象是你的 DAO 吗?

标签: json rest


【解决方案1】:

只需将以下两个注释添加到服务器站点上的 FollowResult 类。

@XmlRootElement(name="followresult")
@XmlAccessorType(XmlAccessType.PROPERTY)

【讨论】:

    猜你喜欢
    • 2017-07-05
    • 2015-08-29
    • 2018-10-13
    • 2018-07-25
    • 2016-09-09
    • 2013-01-16
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多