0
当我发布我的追随者对象到我的其他电话时,我得到了415错误,但我不明白为什么?我没有添加dao和其他值的私有字段,因为它只是一个biger文件,我只在这个帖子中放置了调用和客户端和http处理程序的代码。发布FollowResult对象返回HTTP错误415不支持的媒体类型
错误消息:
发送 'POST' 请求URL:http://localhost:8080/KwetterBart-web/api/user/startfollow 响应代码:415 产生java.io.IOException:服务器返回的HTTP响应代码:415网址:http://localhost:8080/KwetterBart-web/api/user/startfollow
这里是我的类:
方法收到我的REST调用我认为这个问题是值得用这种方法的消耗:
@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;
}
的代码发布到我的休息服务
客户端: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;
}
}
FollowResult对象是您的DAO吗? – Laurence