2016-05-10 11 views
0

私のフォローアップオブジェクトを休憩に投稿すると、415エラーが表示されますが、なぜ表示されませんか?私はDAOのプライベートフィールドを追加していないし、他の価値のbecausは、私はコールとクライアントとHTTPハンドラのコードをこのポストに入れるより大きなファイルです。FollowResultオブジェクトを投稿するとHTTPエラー415が返されますサポートされていないメディアタイプ

エラーメッセージ:http://localhost:8080/KwetterBart-web/api/user/startfollow 応答コード:415 にjava.io.IOException:

がURLに 'POST' リクエストを送信するサーバは、HTTP応答コードを返しました:URLの415:ここで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(); 
    } 
012:

方法私の残りのコールを受ける私はこの問題は、この方法の消費で何かだと思います

HTTPHandlerのためのクラス:私のRESTサービスにコードを投稿

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

クライアント側:FollowResultの

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); 
      } 
    } 

Objectクラス私は私のクライアント側で、私のサーバー側でこのクラスを持っています:

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; 
    } 
} 
+0

FollowResultオブジェクトはDAOですか? – Laurence

答えて

0

だけで、サーバサイト上FollowResultクラスに次の2つの注釈を追加。

@XmlRootElement(name="followresult") 
@XmlAccessorType(XmlAccessType.PROPERTY) 
関連する問題