在Atmosphere框架中将JSON从Javascript客户端发布到Jersey资源
我在谷歌上搜索了几个小时,试图让它工作……问题是服务器接收的数据不是在Atmosphere框架中将JSON从Javascript客户端发布到Jersey资源,java,javascript,json,jersey,atmosphere,Java,Javascript,Json,Jersey,Atmosphere,我在谷歌上搜索了几个小时,试图让它工作……问题是服务器接收的数据不是JSON,而是文本。这是POJO package my.package; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class TestConfig { private String firmID; private String traderID; priva
JSON
,而是文本。这是POJO
package my.package;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class TestConfig {
private String firmID;
private String traderID;
private String userID;
public TestConfig() {};
...
}
包含以下内容的Javascript客户端:
function callbackForTest(response) {
console.log("Call to callbackForTest");
if (response.state == "opening" && response.status == 200) {
//push request data
if (connectedEndpoint[0] == null) {
console.log("[DEBUG] Connected endpoint for " + value + "is null!");
//disable button
$(value).attr('disabled','');
$.atmosphere.unsubscribe();
return false;
}
// push ( POST )
connectedEndpoint[0].push(JSON.stringify(
{
operation : "RUN",
firmID : $('#firmID').val(),
userID : $('#userID').val(),
traderID : $('#traderID').val(),
protocol : $('#protocol').val(),
group1 :
}
));
}
}
function subscribeUrl(jobName, call, transport) {
var location = subscribePath + jobName.id;
return subscribeAtmosphere(location, call, transport);
}
function globalCallback(response) {
if (response.state != "messageReceived") {
return;
}
}
function subscribeAtmosphere(location, call, transport) {
var rq = $.atmosphere.subscribe(location, globalCallback, $.atmosphere.request = {
logLevel : 'debug',
transport : transport,
enableProtocol: true,
callback : call,
contentType : 'application/json'
});
return rq;
}
function sendMessage(connectedEndpoint, jobName) {
var phrase = $('#msg-' + jobName).val();
connectedEndpoint.push({data: "message=" + phrase});
}
// Run Test handlers
$("input[name='runButtons']").each(function(index, value){
$(value).click(function(){
//disable button
$(value).attr('disabled','disabled');
// connect (GET)
connectedEndpoint[index] = subscribeUrl(value, callbackForTest, transport);
});
});
我已包括此屏幕截图中显示的LIB:
这是我的web.xml(它的一部分)
com.sun.jersey.api.json.POJOMappingFeature
真的
泽西岛资源
@Path("/subscribe/{topic}")
@Produces({MediaType.APPLICATION_JSON, "text/html;charset=ISO-8859-1", MediaType.TEXT_PLAIN})
public class Subscriber {
private static final Logger LOG = Logger.getLogger(Subscriber.class);
@PathParam("topic")
private Broadcaster topic;
@GET
public SuspendResponse<String> subscribe() {
LOG.debug("GET - OnSubscribe to topic");
SuspendResponse<String> sr = new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(topic).outputComments(true)
.addListener(new EventsLogger()).build();
return sr;
}
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
@Broadcast
public Broadcastable publish( TestConfig t) {
LOG.debug("POST");
String s = t.getFirmID();
return new Broadcastable(s, "", topic);
}
如果我将内容类型设置为
application/json
,为什么会发送纯文本?让Jersey资源读取JSON的正确方法是什么?我最终通过两个更改实现了这一点:
查看示例后,我将此init param
添加到web.xml
中的AtmosphereServlet
,以解决Tomcat中的text/plain
问题:
<init-param>
<param-name>org.atmosphere.websocket.messageContentType</param-name>
<param-value>application/json</param-value>
</init-param>
org.atmosphere.websocket.messageContentType
应用程序/json
我没有在大气文件中看到这一点。如果是这样的话,本可以节省大量时间,但遗憾的是,API的文档结构混乱且缺乏
此外,我还需要使用jar来确保包含与Jersey相关的所有内容,包括
Jersey json.jar
。在那之后,它成功了!希望这能帮助其他可能遇到相同或类似问题的人。看起来您的客户机将TestConfig对象发布为JSON,但内容类型为“text/plain”。如果您使用Firebug或类似工具来监视实际发布到/subscribe/{topic},1)对于请求头内容类型,您看到了什么值;2)实际的请求体是什么样子的?我正在使用var contentType='application/json'
订阅大气(请参见Javascript代码块中的subscribeAtmosphere
)。是否有其他方法可以传递JSON对象?哦,是的,我现在看到了。不过,您在Firebug、Fiddler、Chrome开发工具或您选择的调试器中实际看到了什么样的POST请求头和主体?知道了这一点,我们至少可以将问题缩小到客户端与服务器之间。对不起,是的……我也在Chrome调试器和Firebug中检查了这一点。我实际上看不到网络选项卡下的POST
。只需调用subscribe
时出现的GET
。通常,当调用push
时,应该有一个POST
,但它不会发生在调试器跟踪中。这就好像在Jersey资源中创建TestConfig对象的失败(publish(TestConfig t
)阻止了文章的完成。或者类似的事情。。。
<init-param>
<param-name>org.atmosphere.websocket.messageContentType</param-name>
<param-value>application/json</param-value>
</init-param>