Dropwizard. Send Json and make jersey + jackson recognize class
我是杰克逊和泽西的新手。已经完成了一些使用这些库的程序。但是我有一个问题我找不到解决方案。
1 2 3 4 5 6 7 8 |
这是在我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $('#newTicketSubmit').click(function () { var jsonData = [ {'topic':$('#inputTopic').val()}, {'priority':$('#inputPriority option:selected').val()}, {'assigned':$('#inputAssign option:selected').val()}, {'text':$('#inputText').val()}, {'solved':0}, {'date':'No date set'} ]; $.ajax({ type: 'POST', url: '/ticket/submit', // The key needs to match your method's input parameter (case-sensitive). data: JSON.stringify(jsonData), processData: false, contentType: 'application/json; charset=utf-8', success: function(data){alert(data);}, failure: function(errMsg) { alert(errMsg); } }); }); |
这就是我的 javascript 文件的样子。它确实调用了服务器。但我不是 100% 确定我是否正确地做所有事情
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public class Ticket { public Ticket() { /* Empty */ } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @JsonProperty private int id; @NotEmpty @JsonProperty private String topic; @NotEmpty @JsonProperty private String text; @NotNull @JsonProperty private int solved; @NotNull @JsonProperty private int priority; @NotNull @JsonProperty private int assigned; @NotEmpty @JsonProperty private String date; |
最后这里是我的
我希望我的项目了解的是,当我从 javascript 应用程序发送内容时。然后服务器将了解如何从该 json 数据创建 Ticket 类。如果我理解正确。这应该是可能的。但我以某种方式做错了什么。
任何可以带我走上正确道路的帮助都非常受欢迎
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | com.sun.jersey.spi.container.ContainerResponse: A message body writer for Java class io.dropwizard.jersey.errors.ErrorMessage, and Java type class io.dropwizard.jersey.errors.ErrorMessage, and MIME media type application/octet-stream was not found. The registered message body writers compatible with the MIME media type are: */* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.server.impl.template.ViewableMessageBodyWriter com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General application/octet-stream -> com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider |
异常的Stracktrace:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException:Java 类 io.dropwizard.jersey.validation.ValidationErrorMessage 和 Java 类型类 io.dropwizard.jersey.validation 的消息体编写器。 ValidationErrorMessage,并且未找到 MIME 媒体类型 application/octet-stream。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:284) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1515) at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83) at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:348) at io.dropwizard.jetty.BiDiGzipFilter.doFilter(BiDiGzipFilter.java:127) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1486) at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1486) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:519) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1097) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:448) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1031) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:173) at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:92) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:162) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:446) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:271) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:246) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532) at java.lang.Thread.run(Thread.java:745) |
好的,所以我昨天找到了解决问题的方法。问题是我对 Json data
的实现
1 2 3 4 5 6 7 8 | var jsonData = { 'topic':$('#inputTopic').val(), 'priority':parseInt($('#inputPriority option:selected').val()), 'assigned':parseInt($('#inputAssign option:selected').val()), 'text':$('#inputText').val(), 'solved':0, 'date':'No date set' }; |
这是一个在 javascript 中工作的 Json 数据示例。