下拉向导。发送Json让jersey+jackson认得class

Dropwizard. Send Json and make jersey + jackson recognize class

我是 jackson and jersey 的新手。已经完成了一些使用这些库的程序。但是我有一个问题,我找不到解决方案。

  @POST
  @Path("/submit")
  @Consumes(MediaType.APPLICATION_JSON)
  public void submitTicket(Ticket ticket)
  {
    System.out.println("*** SUBMIT A NEW TICKET ***");
    System.out.println("Ticket: " + ticket);
  }

这是我的 ticketResource class。我正在尝试将 json 从我的网络应用程序发送到服务器。但我只会在尝试时出错。

    $('#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% 确定我是否正确地做所有事情

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;

最后这是我的 Ticket class 的片段 我想让我的项目理解的是,当我从 javascript 应用程序发送内容时。然后服务器将了解如何从该 json 数据创建票证 class。如果我理解正确的话。那应该是可能的。但我不知何故做错了什么。

非常欢迎任何可以帮助我走上正确道路的帮助

    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

异常跟踪:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: Java class io.dropwizard.jersey.validation.ValidationErrorMessage 和 Java 类型 class 的消息正文编写器io.dropwizard.jersey.validation.ValidationErrorMessage,未找到 MIME 媒体类型 application/octet-stream。

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.run(QueuedThreadPool.java:532)
at java.lang.Thread.run(Thread.java:745)

好的,所以我昨天找到了解决问题的方法。问题是我对 Json 数据

的实现
   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 数据的示例。