Reputation: 97
When i post my follower object to my rest call i get a 415 error but i dont see why? I did not add the private fields of the dao's and other value's becaus its a biger file i only put the code of the calls and client and http handler in this post.
Error message:
Sending 'POST' request to URL : http://localhost:8080/KwetterBart-web/api/user/startfollow Response Code : 415 java.io.IOException: Server returned HTTP response code: 415 for URL: http://localhost:8080/KwetterBart-web/api/user/startfollow
Here are my classes :
Method that receive my rest call i think the problem is something with the consumes of this method:
@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();
}
Class for 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;
}
Client side that post the code to my rest service:
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 class of FollowResult i have this class on my client side and on my server side:
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;
}
}
Upvotes: 0
Views: 705
Reputation: 339
Just add the following two annotations to your FollowResult class on the server site.
@XmlRootElement(name="followresult")
@XmlAccessorType(XmlAccessType.PROPERTY)
Upvotes: 0