【问题标题】:RestEasy method receiving null as parameters in case of POST method and working fine for Get MethodRestEasy 方法在 POST 方法的情况下接收 null 作为参数并且对 Get 方法工作正常
【发布时间】:2015-04-22 19:15:06
【问题描述】:

我创建了一个 RestEasy Web 服务,并使用 Get Method 并使用 http://localhost:8080/rest/search?name=foo 之类的 URI 将参数传递给服务,并通过使用 @QueryParam,它工作正常: 注意:我正在使用 ajax 调用来调用 Web 服务

@GET
@Path("/search")
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces( MediaType.TEXT_PLAIN)
public JSONArray getDetails(@QueryParam("name") String name) {
         //my code here 
}

我的 ajax 调用

$.ajax({
    type : 'get',
        url : searchURL,
        headers : { 'SM_USER' : userId  },
        dataType : 'json',
        success : function(data) {

现在我必须将方法从 GET 更改为 POST,在这种情况下,它不接受 URI 参数,现在我以不同的方式尝试了它,例如: 现在我通过 ajax 数据传递值。 Ajax 调用:

 $.ajax({
    type : 'post',
    url : searchSuppURL,
    headers : { 'SM_USER' : userId},
    contentType: "application/json",
    data: { 'name' : 'foo'},
    dataType : 'json'
success : function(data) {

网络服务代码:

@POST
@Path("/search")
//@Consumes({"application/x-www-form-urlencoded"})
@Consumes({"application/xml", "application/json", "text/plain"})
@Produces( MediaType.TEXT_PLAIN)
public JSONArray getDetails(String name) {

数据现在可供服务使用,但格式不正确我已将其 sysout

System.out.println("name...." +name);

不是打印 name....foo 而是打印 name....name=foo

我没有得到我缺少的任何建议...

提前致谢

【问题讨论】:

    标签: ajax json rest jax-rs resteasy


    【解决方案1】:

    我想简单地获取一个字符串值,然后只需使用

    @Consumes({"application/x-www-form-urlencoded"})
    

    就像你试图做的那样。去掉 ajax 请求中的contentType(默认为application/x-www-form-urlencoded。并在资源方法中添加@FormParam注解

    public JSONArray getDetails(@FormParam("name") String name) {
    

    【讨论】:

    • 嗨,它在本地工作正常,因为我将它移到 DEV 环境中它停止工作相同的参数是 null 任何建议..
    猜你喜欢
    • 1970-01-01
    • 2012-05-08
    • 2013-06-16
    • 2020-08-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多