[英]Not able to send JSON object to Ajax from Jersey REST web service
I'm writing a simple Jersey REST web service where I'm consuming JSON object and sending another JSON object. 我正在编写一个简单的Jersey REST Web服务,其中使用了JSON对象并发送了另一个JSON对象。 This is just for the workaround. 这仅用于解决方法。 It throws the following error. 它引发以下错误。
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) org.codehaus.jackson.map.JsonMappingException:未找到类org.json.JSONObject的序列化器,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))
$('#click').click(function() {
$.ajax({
url: "http://localhost:8080/test/rest/myService/jsonpost",
method: "POST",
data: jsonObj,
dataType: 'application/json',
contentType: "application/json",
success: function(result){
alert(result);
},
error(){
alert("error");
console.log('Error');
}
});
});
My code is : 我的代码是:
@POST
@Path("/jsonpost")
@Consumes(MediaType.APPLICATION_JSON)
//@Produces(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON })
public Response consumeJSON( Track track ) {
System.out.println("In post example");
String output = track.toString();
System.out.println("Post data"+output);
JSONObject obj = new JSONObject();
obj.put("1", "Shane");
obj.put("2", "Bond");
System.out.println(obj);
return Response.status(200).entity(obj.toString()).build();
//return Response.status(200).entity("success").build();
}
Whole error log 整个错误日志
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.failForEmpty(StdSerializerProvider.java:90)
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:63)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:587)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:245)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1145)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:520)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
如果使用org.codehaus.jackson.map.ObjectMapper,则请在代码中使用以下几行。
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
Jackson is meant to be used with POJOs (like your Track
class). Jackson可以与POJO(例如Track
类)一起使用。 It doesn't know anything about or how to serialize those org.json
objects. 它不知道任何有关如何org.json
对象或如何序列化这些对象的信息。 I would recommend to just use POJOs. 我建议只使用POJO。 The application will be a lot more maintainable with domain classes that clearly defining the domain. 使用明确定义域的域类,该应用程序将具有更大的可维护性。
With that being said, it is still possible to make it work with the org.json
object or arbitrary key/value collections. 话虽如此,仍然有可能使其与org.json
对象或任意键/值集合一起工作。
You can simple return a String
instead of the JSONObject
您可以简单地返回一个String
而不是JSONObject
return Response.ok(obj.toString());
You can use a Map<String, String>
instead. 您可以改用Map<String, String>
。 Jackson knows how to serialize those. 杰克逊知道如何序列化这些。
There is a support module for org.json
, but it's only available for Jackson 2.x ( com.fasterxml
not org.codehaus)
. 有一个org.json
支持模块 ,但仅适用于Jackson 2.x( com.fasterxml
而不是org.codehaus)
。
If you want to use Jackson 2.x, you can check out this post . 如果您想使用Jackson 2.x,可以查看这篇文章 。 Then you need to register the JsonOrgModule
seen in the earlier link. 然后,您需要注册在前面的链接中看到的JsonOrgModule
。 You can do that in a ContextResolver
, as seen in this post . 你可以做,在一个ContextResolver
,在看到这个帖子 。 Going this route, here is your checklist 走这条路线,这是您的清单
ContextResolver
where you register the JsonOrgModule
. 实施ContextResolver
在您注册JsonOrgModule
。 This will allow Jackson to know about the org.json
objects. 这将使Jackson能够了解org.json
对象。 If you need help configuring the last option, please update your post with your web.xml and your dependencies 如果您在配置最后一个选项时需要帮助,请使用web.xml和依赖项更新您的帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.