a problem to pack jar with maven assembly and shade plugin

the problem appears when I package the jar file in a jetty and jersey project following the reference :maven 打jar包依赖问题 

In idea, an http procedure with request and response works

With maven pack plugin, it does not work with http 415 response (Unsupported Media Type)

2

Firstly, I compare the http raw of client in both two situcation with fiddler, as a result, no difference

3

415 http response means the server can not deal with the http body with the value of 'Content-Type' field in the http client header

in this case, json is used to unsearize and searize the http body

so we guess, the procedure of mvn package missed some class to dear with json

4

why missed ?

maybe the jetty framework use json with java reflect, which contributes to that assembly and shade plugin cannot reach relative class during compile and pack procedure.

In runtime, the framework cannot deal with the json string in http body from client, then send a response of 415

5

it comes to success when we switch to maven-jar-plugin and dependency-plugin which directly copies all the related jars containing required binary class

reference:

http协议简史

json文本协议

https://blog.csdn.net/majinggogogo/article/details/78383772

6

a few days later, it is required to pack in assemby plugin

7

firstly, append a global exception function to jetty and jersey flowing the link - 

https://blog.csdn.net/sotong006/article/details/76216441

https://blog.csdn.net/u013628152/article/details/42677655

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class DeviceExceptionMapper implements ExceptionMapper<Throwable> {

    private Logger logger = LoggerFactory.getLogger(DeviceExceptionMapper.class);

    @Override
    public Response toResponse(Throwable e) {
        logger.error(e.getMessage(), e);
        return Response.status(200).entity(e.getMessage()).build();
    }
}

  

resourceConfig.packages

  

review the logs:

2019-11-26 10:03:50 ERROR DeviceExceptionMapper:18 - HTTP 415 Unsupported Media Type
javax.ws.rs.NotSupportedException: HTTP 415 Unsupported Media Type
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:101)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:499)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Unknown Source)
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=xxxxxxxRequest, genericType=xxxxxxxRequest.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:74)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)
at org.glassfish.jersey.server.internal.inject.EntityParamValueFactoryProvider$EntityValueFactory.provide(EntityParamValueFactoryProvider.java:96)
at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:94)
... 36 more

8

now it is known json lib is absent in serize and unserize to http body byte[]

in regard to https://stackoverflow.com/questions/28644268/java-jersey-415-unsupported-media-type,

Seems to be the main problem is that you don't have a MessageBodyReader configured to handle JSON. You have the jersey-media-json-jackson on the classpath, but you will still need to register the provider. That's why the Unsupported Media Type. If Jersey can't find a provider to handle the conversion, this is the the status you will get.

If you are using web.xml, you can use

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            jersey.web.stackoverflow,   <!-- your package -->
            org.codehaus.jackson.jaxrs  <!-- jackson package -->
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

If you are using a ResourceConfig class, then you can just do

public class AppConfig extends ResourceConfig {
    public AppConfig() {
        register(JacksonFeature.class);
    }
}

 

add 

resourceConfig.register(JacksonFeature.class);

done

原文地址:https://www.cnblogs.com/silyvin/p/11911369.html