我一直在谷歌搜索并试图让它工作几个小时......问题是服务器没有接收数据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);
});
});
我已包含此屏幕截图中显示的库:
这是我的 web.xml(它的一部分)
com.sun.jersey.api.json.POJOMappingFeature true
泽西岛资源
@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);
}
我可以订阅确定。当我尝试推送到服务器时,我得到了这个异常:
A message body reader for Java class com.mx.sailcertifier.TestConfig, and Java type class com.mx.sailcertifier.TestConfig, and MIME media type text/plain was not found.
如果我将内容类型设置为,为什么它会发送纯文本application/json
?让 Jersey 资源读取 JSON 的正确方法是什么?