不允许使用泽西岛方法405
我是其余服务的新手.我正在尝试创建一个接受来自客户端的json字符串的服务.使用JQuery调用此服务时出现405错误. 以下是ws的Java代码:
I am new to the rest services. I am trying to create a service that accepts json string from a client. I am getting 405 error when I am calling this service using JQuery. Below is the Java code for ws:
@POST
@Path("logevent")
@Consumes(MediaType.APPLICATION_JSON)
public boolean logEvent(String obj)
{
System.out.println(obj);
return true;
}
和
@Path("getdata")
@GET
public String getData()
{
return "Hello";
}
用于发布JSON的jQuery代码是:
and jQuery code for posting the JSON is:
var json ="{\"userName\":\"testtest\"}";
var json_data = JSON.stringify(json);
$.ajax({
type: "POST",
url: "http://localhost:8080/log/log/logevent",
// The key needs to match your method's input parameter (case-sensitive).
data: json_data,
contentType: "application/json",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
出了什么问题?该帖子不起作用,但是当我使用URL http://<serverip>/log/log/getdata
点击get时,得到响应.
What is going wrong? The post is not working, however when I hit the get using the URL http://<serverip>/log/log/getdata
I get the response.
JSON MessageBodyReader
能够将JSON流解组到JAXB bean(或POJO)中,但不能解组到String中.创建一个像这样的JAXB bean:
JSON MessageBodyReader
s are able to unmarshal JSON stream into a JAXB bean (or POJO) but not into a String. Create a JAXB bean like:
@XmlRootElement
public class User {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(final String userName) {
this.userName = userName;
}
}
,然后将您的POST
资源方法更改为:
and change your POST
resource method to:
@POST
@Path("logevent")
@Consumes(MediaType.APPLICATION_JSON)
public boolean logEvent(User obj) {}