且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有找到Jersy / Jetty服务器的MessageBodyWriter错误

更新时间:2022-02-22 18:00:56

我在中所述verflow.com/q/37735728/2587435>从命令行运行jar时出现MessessBodyProviderNotFoundException ,有一个服务文件,即 org.glassfish.jersey.internal.spi.AutoDiscoverable ,包含在许多罐子里。此文件的目的是允许泽西岛(和其他第三方)罐子为这些罐子中包含的功能提供一些自动注册。这包括注册 JacksonFeature ,它注册处理(反)序列化的JSON提供程序。

As mentioned in my answer in MessageBodyProviderNotFoundException while running jar from command line, there is a "services file", namely org.glassfish.jersey.internal.spi.AutoDiscoverable, which is included in many jars. The purpose of this file is to allow Jersey (and other third party) jars to provide some auto-registration for features included in those jars. This includes registration of the JacksonFeature, which registers the JSON providers that handle (de)serialization.

问题创建fat(uber)jar是因为只能有一个这样的文件(你不能有多个同名的文件)。因此,在胖罐中包含的所有罐子中,只包含一个服务文件。

The problem with creating fat (uber) jars is that there can only be one of those files (you can't have more than one file of the same name). So with all the jars included in the fat jar, only one service file will be included.

使用Maven,我们将使用maven-shade-plugin,它具有变换器允许转换构建的部分。 shade插件有一个 ServicesResourceTransformer ,它负责将服务文件的内容连接成一个服务文件。您用于Gradle的插件 Shadow 具有相同的功能。您可以通过在配置中调用 mergeServiceFiles()来配置它。我没有真正使用Gradle,但也在这个问题中说明,推荐用于处理服务文件转换和主类清单转换的配置如下:

With Maven, we would use the maven-shade-plugin, which has transformers that allow for transforming parts of the build. The shade plugin has a ServicesResourceTransformer which takes care of concatenating the contents of the service files into one service file. The plugin you are using for Gradle, Shadow, has the same facility. You configure it by calling mergeServiceFiles() in the configuration. I don't really work with Gradle, but also stated in this issue, the recommended configuration for handling both the service files transformation and the main class manifest transformation is the following

shadowJar {
  mergeServiceFiles()
  manifest {
    attributes 'Main-Class': 'com.my.Application'
  }
}

所以我假设,使用上面的配置,你也可以删除

So I am assuming, with the above configuration, you can also remove

jar { manifest { attributes 'Main-Class': "${mainClassName}" } }

因为Shadow插件将负责构建清单。同样,我并没有真正使用Gradle,所以这只是一个假设;但听起来不错。

as the Shadow plugin will take care of building the manifest. Again, I don't really use Gradle, so this is just an assumption; but it sounds right.