今天使用postman测试接口的时候报了个415,流媒体错误;百度了一下其实看到了答案,但是没有注意,所以没有解决,正好组长过来,问了一下,是没有设置请求头的原因。
客户端使用RESTful调用时报status code=415 Unsupported Media Type.
问题产生的原因可能在于客户端,也可能在于服务端.
使用客户端做测试时注意设置Headers,以Firefox的插件RESTClient为例设置如下:
排除客户端设置问题后,如果还是报错415,需要检查服务端设置,以wink为例:
wink框架默认是不集成json的,需要单独制定json依赖才行。pom中加入如下配置依赖就OK了:
<dependency> <groupId>org.apache.winkgroupId> <artifactId>wink-jackson-providerartifactId> dependency>
扩展一,不仅wink实现json时默认不支持json,其他框架如jboss.resteasy时也是默认不支持json的,需要加入
<dependency> <groupId>org.jboss.resteasygroupId> <artifactId>resteasy-jackson2-providerartifactId> dependency>
才能支持json
扩展二:
用来传递参数的实体了必须实现传入参数为空的构造函数,否则可能报如下错误:
客户端错误:com.topsec.tap.rest.base.exception.server.ServerUnknownException: uuid=G2Haiu45vwxgM9KFFXKqef, code=ERR_REST_SERVER_UNKNOWN, message=REST服务器端未知异常!
Error 500
HTTP ERROR: 500
Problem accessing /discover/addTask. Reason:
javax.servlet.ServletException: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.topsec.tap.discoverer.bean.ScanTask]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: HttpInputOverHTTP@18f60fb; line: 1, column: 2]
Powered by Jetty://
服务器端错误:
ERROR o.a.w.s.internal.RequestProcessor - An unhandled exception occurred which will be propagated to the container.
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.topsec.tap.discoverer.bean.ScanTask]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: HttpInputOverHTTP@18f60fb; line: 1, column: 2]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) ~[jackson-jaxrs-1.9.4.jar:1.9.4]
at org.apache.wink.server.internal.registry.ServerInjectableFactory$EntityParam.getValue(ServerInjectableFactory.java:200) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.common.internal.registry.InjectableFactory.instantiate(InjectableFactory.java:68) ~[wink-common-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.CreateInvocationParametersHandler.handleRequest(CreateInvocationParametersHandler.java:44) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleSubResourceMethod(FindResourceMethodHandler.java:183) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleRequest(FindResourceMethodHandler.java:110) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:95) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:53) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:46) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.log.ResourceInvocation.handleRequest(ResourceInvocation.java:92) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.log.Requests.handleRequest(Requests.java:76) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:48) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:207) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:154) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:119) [wink-server-1.4.jar:1.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:800) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:517) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at 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]
at org.eclipse.jetty.server.Server.handle(Server.java:497) [jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112]
at 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]
at 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]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540) [jetty-util-9.2.5.v20141112.jar:9.2.5.v20141112]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
排除客户端设置问题后,如果还是报错415,需要检查服务端设置,以wink为例:
wink框架默认是不集成json的,需要单独制定json依赖才行。pom中加入如下配置依赖就OK了:
<dependency> <groupId>org.apache.winkgroupId> <artifactId>wink-jackson-providerartifactId> dependency>
扩展一,不仅wink实现json时默认不支持json,其他框架如jboss.resteasy时也是默认不支持json的,需要加入
<dependency> <groupId>org.jboss.resteasygroupId> <artifactId>resteasy-jackson2-providerartifactId> dependency>
才能支持json
扩展二:
用来传递参数的实体了必须实现传入参数为空的构造函数,否则可能报如下错误:
客户端错误:com.topsec.tap.rest.base.exception.server.ServerUnknownException: uuid=G2Haiu45vwxgM9KFFXKqef, code=ERR_REST_SERVER_UNKNOWN, message=REST服务器端未知异常!
Error 500
HTTP ERROR: 500
Problem accessing /discover/addTask. Reason:
javax.servlet.ServletException: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.topsec.tap.discoverer.bean.ScanTask]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: HttpInputOverHTTP@18f60fb; line: 1, column: 2]
Powered by Jetty://
服务器端错误:
ERROR o.a.w.s.internal.RequestProcessor - An unhandled exception occurred which will be propagated to the container.
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.topsec.tap.discoverer.bean.ScanTask]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: HttpInputOverHTTP@18f60fb; line: 1, column: 2]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) ~[jackson-mapper-asl-1.9.4.jar:1.9.4]
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) ~[jackson-jaxrs-1.9.4.jar:1.9.4]
at org.apache.wink.server.internal.registry.ServerInjectableFactory$EntityParam.getValue(ServerInjectableFactory.java:200) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.common.internal.registry.InjectableFactory.instantiate(InjectableFactory.java:68) ~[wink-common-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.CreateInvocationParametersHandler.handleRequest(CreateInvocationParametersHandler.java:44) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleSubResourceMethod(FindResourceMethodHandler.java:183) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleRequest(FindResourceMethodHandler.java:110) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:95) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:53) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:46) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.log.ResourceInvocation.handleRequest(ResourceInvocation.java:92) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.log.Requests.handleRequest(Requests.java:76) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:48) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:207) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:154) ~[wink-server-1.4.jar:1.4]
at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:119) [wink-server-1.4.jar:1.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:800) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:517) [jetty-servlet-9.2.5.v20141112.jar:9.2.5.v20141112]
at 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]
at org.eclipse.jetty.server.Server.handle(Server.java:497) [jetty-server-9.2.5.v20141112.jar:9.2.5.v20141112]
at 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]
at 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]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540) [jetty-util-9.2.5.v20141112.jar:9.2.5.v20141112]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]