我想创建一个REST服务器。
我添加了maven依赖项,我的pom.xml
看起来像这样:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>abptc_server</groupId>
<artifactId>abptc_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
</dependencies>
</project>
我添加了maven-assembly-plugin
,因为我想要一个jar,我可以执行:
java -cp target/abptc_server-0.0.1-SNAPSHOT-jar-with-dependencies.jar server.MinimalServerRest
当我在eclipse中运行服务器时,它运行良好,但是当我执行jar时,服务器在一个REST请求后失败:GET http://localhost:9999/employee/getEmployee
错误:
2014-03-20 11:40:46.996:INFO::main: Logging initialized @576ms
2014-03-20 11:40:47.133:INFO:oejs.Server:main: jetty-9.1.z-SNAPSHOT
2014-03-20 11:40:47.176:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@7dbe444b{/,null,AVAILABLE}
2014-03-20 11:40:47.194:INFO:oejs.ServerConnector:main: Started ServerConnector@6bb9db06{HTTP/1.1}{0.0.0.0:9999}
2014-03-20 11:40:47.194:INFO:oejs.Server:main: Started @777ms
márc. 20, 2014 11:40:53 DE com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
rest
márc. 20, 2014 11:40:53 DE com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class rest.EmployeeSvc
class rest.StudentSvc
márc. 20, 2014 11:40:53 DE com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
márc. 20, 2014 11:40:54 DE com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM'
márc. 20, 2014 11:40:54 DE com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class dto.Employee, and Java type class dto.Employee, and MIME media type application/json was not found.
The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
márc. 20, 2014 11:40:54 DE com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class dto.Employee, and Java type class dto.Employee, and MIME media type application/json was not found.
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:284)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:711)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:552)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:219)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1112)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:479)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1046)
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:462)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:281)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
at org.eclipse.jetty.io.AbstractConnection$1.run(AbstractConnection.java:505)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:744)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class dto.Employee, and Java type class dto.Employee, and MIME media type application/json was not found.
... 24 more
我搜索了SO的答案:
https://stackoverflow.com/a/12427046/598280和https://stackoverflow.com/a/6027161/598280我已添加jersey-json
依赖性。
https://stackoverflow.com/a/11936282/598280我没有web.xml
并且服务器运行良好,当我在eclipse中执行时,问题出在maven上。
https://stackoverflow.com/a/13108540/598280同样。
根据Mark Needham blog,我的服务器类看起来很好:
ServletHolder sh = new ServletHolder(ServletContainer.class);
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
// Set the package where the services reside
sh.setInitParameter("com.sun.jersey.config.property.packages", "rest");
sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
Server server = new Server(9999);
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.addServlet(sh, "/*");
server.start();
server.join();
我该如何解决这个问题?
我的打包罐看起来像这样:
我没有检查所有依赖性(它有23个个体的依赖性),但从这里看起来很好。我应该检查所有吗?
这是maven clean install
输出:http://pastebin.com/Whk2qr2i
答案 0 :(得分:1)
META-INF/MANIFEST.MF
个文件,但实际上只有一个。这可能适用于其他文件。这可能会导致奇怪的行为
我会尝试使用maven将它们构建为单独的JARS,然后尝试运行并查看是否有效。您可以使用程序集插件构建一个ZIP,其中包含您的应用程序JAR和lib文件夹中的所有JAR文件,以及运行它的脚本。例如。 zip的内容如下:
abptcserver/
lib/
dependency1.jar
dependency2.jar
dependency3.jar
abptcserver.jar
run.sh
run.bat
然后,您可以配置shell和批处理脚本,以使用类路径上的lib目录中的所有JARS运行服务器。我想你会以这种方式获得更少的意外行为,它也更强大,更便于用户运行
答案 1 :(得分:0)
使用maven的shade插件而不是程序集插件。
它不会覆盖类,即使在不同的jar中存在相同的名称,也会解决此问题。