当我将我的追随者对象发布到我的休息电话时,我得到415错误,但我不明白为什么?我没有添加dao的私有字段和其他值,因为它是一个biger文件我只在这篇帖子中放置了调用代码和客户端以及http处理程序。
错误讯息:
发送' POST'请求网址:http://localhost:8080/KwetterBart-web/api/user/startfollow 回应代码:415 java.io.IOException:服务器返回HTTP响应代码:415为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;
}
}
答案 0 :(得分:0)
只需将以下两个注释添加到服务器站点上的FollowResult类。
@XmlRootElement(name="followresult")
@XmlAccessorType(XmlAccessType.PROPERTY)