我尝试在pom.xml中添加以下依赖项
<!-- Jersey-Json -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
以及下面的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- <init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param> -->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
&#13;
按照其他帖子。 但我还是得到了
2015-03-31T15:52:08.771 + 0530 WARN:loggerName =&#34; o.j.resteasy.core.ExceptionHandler&#34; threadName =&#34; qtp1558867745-18&#34; txnId =&#34;&#34;未能执行 javax.ws.rs.NotSupportedException:找不到类型的消息体读取器:类com.apple.ist.curo.es.data.cdto.RequestCDTO内容类型:text / plain; charset = UTF-8 在org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.throwReaderNotFound(ServerReaderInterceptorContext.java:52)〜[resteasy-jaxrs-3.0.7.Final.jar:na] at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50)〜[resteasy-jaxrs-3.0.7.Final.jar:na] at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:150)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:89)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:112)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:288)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:242)〜[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:229)〜[resteasy-jaxrs-3.0.7.Final.jar:na] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)[resteasy-jaxrs-3.0.7.Final.jar:na] 在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)[resteasy-jaxrs-3.0.7.Final.jar:na] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(httpServletDispatcher.java:51)[resteasy-jaxrs-3.0.7.Final.jar:na] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)[javax.servlet-api-3.1.0.jar:3.1.0] 在org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:800)[jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)[jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)[jetty-security-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)[jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.Server.handle(Server.java:497)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)[jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.io.AbstractConnection $ 2.run(AbstractConnection.java:540)[jetty-io-9.2.5.v20141112.jar:9.2.5.v20141112] at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620)[jetty-util-9.2.5.v20141112.jar:9.2.5.v20141112] 在org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:540)[jetty-util-9.2.5.v20141112.jar:9.2.5.v20141112] 在java.lang.Thread.run(Thread.java:744)[na:1.7.0_51]
我的代码如下:
@POST
@Path(PERSON_SALARY_PATH)
@Produces(MediaType.APPLICATION_JSON)
public String getSalary(@RequestBody RequestCDTO requestCDTO) {
答案 0 :(得分:5)
在我的代码中,它在添加依赖项
之后起作用<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.19.Final</version>
</dependency>
答案 1 :(得分:2)
添加@Consumes(MediaType.APPLICATION_JSON)并确保设置请求标头“Content-Type:application / json”。前者我认为不是必需的,但无论如何你应该离开它。后者是最重要的。出于某种原因,您的请求将以“text / plain”标题发送出去
peeskillet的上述评论是正确的答案。