当我将我的follower对象发布到我的rest调用时,我得到了一个415错误,但我不明白为什么?我没有添加dao和其他值的私有字段,因为它是一个更大的文件,我在这篇文章中只放了调用、客户端和http处理程序的代码。
错误消息:
正在向URL发送“POST”请求:http://localhost:8080/KwetterBart-web/api/user/startfollow响应代码:415Java。io。IOException:服务器为URL返回了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;
}
将代码发布到我的Rest服务的客户端:
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类中。
@XmlRootElement(name="followresult")
@XmlAccessorType(XmlAccessType.PROPERTY)
问题内容: 我正在使用JSON请求调用REST服务,并且它以错误响应。 请求内容类型设置为。 如果我在请求中不包含JSON对象,则效果很好。我正在使用JSON库。 我尝试使用几个不同的库,但这没什么区别。 有人可以帮我解决这个问题吗? 这是我的代码: 的值为: 问题答案: 不确定原因,但是从中 删除行解决了该问题。
我用JSON请求调用REST服务,它用HTTP 415“不支持的媒体类型”错误进行响应。 请求内容类型设置为。 如果我在请求中不包含JSON对象,它就可以正常工作。我正在使用JSON库。 我试着使用了几个不同的库,但没有什么不同。 有人能帮我解决这个问题吗? 这是我的代码: 是:
问题内容: 我正在用JSON请求调用REST服务,它给出了Http 415“不支持的媒体类型”错误。 请求内容类型设置为(“ Content-Type”,“ application / json; charset = utf8”)。 如果我在请求中不包含Json对象,则效果很好。我正在使用json的google-gson-2.2.4库。 我尝试使用几个不同的库,但这没什么区别。 有人可以帮我解决这
我已经创建了一个示例web服务来进行post调用。 我使用的是Jersey JAX-RS和Maven。
我使用jax-rs和jersey创建了一个REST webservice,它应该在POST请求上使用JSON。我的web服务类如下所示: 我的歌曲课: 我有点用RESTClient,嗯,rest Client...以下是我发送的内容的截图: 当我发送它时,我得到415不受支持的媒体类型错误。有人知道为什么吗?