为什么我的Web应用程序在Glassfish上运行,但在Tomcat服务器上不运行?

时间:2015-10-12 05:54:43

标签: java maven tomcat

我关注Maven项目:

的pom.xml

<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>

的web.xml

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <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>ca.gatin.todoapi</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

REST资源

@Path("/accounts")
public class AccountResource {
    AccountService accountService = new AccountService();

    @GET
    @Path("/test")
    @Produces(MediaType.TEXT_HTML)
    public String wsTest() {
        return "Web Service works!";
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Account> getAccounts() {
        return accountService.getAccounts();
    }

}

和模型

@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() {}

...
}

当我在GlassFish 4.0上部署这个项目时,所有API都可以工作,但是当我在Tomcat 7上部署它时,这个API可以工作: http://localhost:8080/todoapi/webapi/accounts/test ,但这个不起作用: http://localhost:8080/todoapi/webapi/accounts 如果给出不同的错误,无论我尝试什么:

  

com.sun.jersey.api.MessageException找不到MIME媒体类型application / json。

     

org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteTo SEVERE:找不到媒体类型= application / json,

的MessageBodyWriter

1 个答案:

答案 0 :(得分:0)

好的,所以你的错误说明了一切:

MessageBodyWriter not found for media type=application/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>