不允许使用泽西岛方法405 [英] Jersey Method not allowed 405

查看:75
本文介绍了不允许使用泽西岛方法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 MessageBodyReaders 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) {}

这篇关于不允许使用泽西岛方法405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆