Intellij上的项目工作正常,在pom中定义了com.sun.jersey服务器,核心和json的依赖项。 然后我创建了jar(为此我必须将META-INF / MANIFEST.MF从/ src / main / java移动到/ src / main / resources),这似乎没问题(在jar中嵌入的文件夹中)我可以在maven下找到所有正确的依赖项。 然后我启动jar并正确启动...但是,在运行时,当程序试图从rest api获取json时,我收到以下错误:
May 10, 2018 9:33:25 AM com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: A message body reader for Java class java.lang.String, and Java type class java.lang.String, and MIME media type application/json was not found
May 10, 2018 9:33:25 AM com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: The registered message body readers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.lang.String, and Java type class java.lang.String, and MIME media type application/json was not found
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:630)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:586)
at com.dataCollection.executor.Downloader.download(Downloader.java:49)
at com.dataCollection.executor.Downloader.run(Downloader.java:34)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
由于某种原因,它找不到媒体类型application / json。但是当我从Intellij启动主类时,它运行正常。
以下是程序调用远程服务的代码:
Client client = Client.create();
WebResource webResource = client.resource("https://...(correct address)...");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
String output = response.getEntity(String.class);
JSONObject jsonObject = new JSONObject(output);
这是pom:
<groupId>groupId</groupId>
<artifactId>CollectDataFromREST</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>true</createDependencyReducedPom>
<dependencyReducedPomLocation>
${java.io.tmpdir}/dependency-reduced-pom.xml
</dependencyReducedPomLocation>
<relocations>
<relocation>
<pattern>com.acme.coyote</pattern>
<shadedPattern>hidden.coyote</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dataCollection.StartDataCollection</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>