Java 为什么我的web应用程序可以在Glassfish上运行,但不能';Tomcat服务器上的t?
我有以下Maven项目: pom.xmlJava 为什么我的web应用程序可以在Glassfish上运行,但不能';Tomcat服务器上的t?,java,maven,tomcat,Java,Maven,Tomcat,我有以下Maven项目: pom.xml <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version&
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
<properties>
<param>local</param>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>todoapi</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/${param}</directory>
</resource>
</resources>
</build>
当我在GlassFish 4.0上部署此项目时,所有API都可以工作,但当我在Tomcat 7上部署此项目时,此API可以工作:
,但这个不起作用:
如果给出不同的错误,无论我如何尝试:
未找到com.sun.jersey.api.MessageException MIME媒体类型应用程序/json。org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo严重:找不到媒体类型为application/json的MessageBodyWriter
好吧,你的错误说明了一切: 当您使用基于JAXRS的框架时,需要消息编写器来生成json/xml。很少有web/应用程序服务器提供默认的消息编写器,例如Glassfish。但并不是所有服务器都提供,就像第二种情况一样,Tomcat不提供 您需要为消息json消息编写器(如jersey json等)添加依赖项。因为您使用的是maven,所以应该这样做:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>{latest_version}</version>
</dependency>
泽西岛
泽西json
{最新版本}
好,您的错误说明了一切:
当您使用基于JAXRS的框架时,需要消息编写器来生成json/xml。很少有web/应用程序服务器提供默认的消息编写器,例如Glassfish。但并不是所有服务器都提供,就像第二种情况一样,Tomcat不提供
您需要为消息json消息编写器(如jersey json等)添加依赖项。因为您使用的是maven,所以应该这样做:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>{latest_version}</version>
</dependency>
泽西岛
泽西json
{最新版本}
post your error/stack trace我相信Glassfish有一些JAR默认情况下不包括在Tomcat中。从您的POM来看,甚至有一节说要取消注释以获得JSON支持。你试过了吗?现在,当我试着在Tomcat上运行它时,我遇到了这个错误:2015年10月11日11:01:38 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo严重:媒体类型=应用程序/json,类型=类java.util.ArrayList找不到MessageBodyWriter,genericType=java.util.List.Now我的Maven项目可以在Tomcat和Glassfish上成功运行。让我困惑的是,Jersey 2使用的是org.Glassfish.Jersey。。。我想,如果有glassfish这个词,它应该只在glassfish服务器上使用,我试着不在Tomcat上使用它。我相信Glassfish有一些罐子,默认情况下Tomcat中没有这些罐子。从您的POM来看,甚至有一节说要取消注释以获得JSON支持。你试过了吗?现在,当我试着在Tomcat上运行它时,我遇到了这个错误:2015年10月11日11:01:38 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo严重:媒体类型=应用程序/json,类型=类java.util.ArrayList找不到MessageBodyWriter,genericType=java.util.List.Now我的Maven项目可以在Tomcat和Glassfish上成功运行。让我困惑的是,Jersey 2使用的是org.Glassfish.Jersey。。。我想,如果有glassfish这个词,它应该只在glassfish服务器上使用,我试着不在Tomcat上使用它。但这是错误的,我可以在两者上使用它。好吧,我已经添加了:com.sun.jersey json 1.18.1 oops,一秒钟,我做了一些其他更改,它成功了!让我检查一下到底是什么解决了这个问题。我已经取消了注释,它只是起作用了:org.glassfish.jersey.media jersey media moxy,所以不需要添加jersey json,因为它是jersey 1,但我使用的是org.glassfish.jersey,它是jersey 2。我添加了:com.sun.jersey jersey json 1.18.1哦,1秒,我做了一些其他的改变,它成功了!让我看看到底是什么解决了这个问题。我已经取消了注释,它只是起作用了:org.glassfish.jersey.media jersey media moxy,所以不需要添加jersey json,因为它是jersey 1,但我使用的是org.glassfish.jersey,它是jersey 2
@XmlRootElement
public class Account {
private int id;
private String firstName;
private String lastName;
private String email;
private String password;
private boolean isActive;
private Date dateCreated;
private Date dateLastModified;
// required for JAXB
public Account() {}
...
}
MessageBodyWriter not found for media type=application/json
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>{latest_version}</version>
</dependency>