【问题标题】:Consume Cookie and JSON with JAX-RS Jersey Service使用 JAX-RS Jersey 服务使用 Cookie 和 JSON
【发布时间】:2015-06-16 02:46:40
【问题描述】:

我正在使用 jQuery 代码和方法 POST 将这些数据发送到 Restful Web Service (Jersey):

var dataString = {"id":1,"status":"passed","session":"nothing"};
$.post("https://localhost:8443/pv01/ws/user/cookie", dataString);

有了这些数据,我将发送一个 cookie。 te cookie 中的数据来自外部 API。

我面临的问题是如何同时接收cookie值和dataString。

这是我读取 Cookie 的 Java 代码:

@POST
@Path("cookie")    
public String cookie(@CookieParam("L14c") String str) {        
    Logger.getLogger(Main.class.getName()).log(Level.INFO, "message : " + str );
    return str;
}

对于数据,我可以这样做:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("cookie")
public String cookie(DataString dataString) {        
    Logger.getLogger(Main.class.getName()).log(Level.INFO, "message : " + dataString );
    return "ok";
}

但是当我结合这两种方法来接受 cookie 和 JSON dataString 时,我得到了错误 415,不支持的媒体类型!

我尝试查看 HTTP 标头,但我只能访问 cookie。

【问题讨论】:

    标签: java rest cookies jersey jax-rs


    【解决方案1】:

    问题在于 jQuery 请求。看起来Content-Type 默认为application/x-www-form-urlencoded。您应该使用像 Firebug 这样的浏览器调试器。更容易发现这些东西。

    根据我的测试,它应该适用于类似的东西

    $.ajax({
        url: theUrl,
        type: "POST",
        data: JSON.stringify(dataString),
        dataType: "json",
        contentType: "application/json",
        success: function(response) {
            alert(JSON.stringify(response));
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多