泽西服务JSP:找不到媒体类型

时间:2017-05-03 15:11:59

标签: java maven jsp jersey

我需要使用Jersey framework 2.22版来呈现JSP页面。

尝试搜索网络,文档,但我无法让它们工作。

目前我完成的只是错误:

19:03:45,891 SEVERE [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] (default task-48) MessageBodyWriter not found for media type=text/html, type=class com.sun.jersey.api.view.Viewable, genericType=class com.sun.jersey.api.view.Viewable.

这是我的代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--<servlet>-->
        <!--<servlet-name>Jersey Web Application</servlet-name>-->
        <!--<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>-->
        <!--<init-param>-->
            <!--<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>-->
            <!--<param-value>true</param-value>-->
        <!--</init-param>-->
        <!--<init-param>-->
            <!--<param-name>jersey.config.server.provider.packages</param-name>-->
            <!--<param-value>io.cnick.api.controllers</param-value>-->
        <!--</init-param>-->
        <!--<load-on-startup>1</load-on-startup>-->
    <!--</servlet>-->

    <!--<servlet-mapping>-->
        <!--<servlet-name>Jersey Web Application</servlet-name>-->
        <!--<url-pattern>/*</url-pattern>-->
    <!--</servlet-mapping>-->

    <filter>
        <filter-name>Jersey Filter</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.cnick.api.controllers</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.servlet.filter.staticContentRegex</param-name>
            <param-value>/(resources|css|js|page|(WEB-INF/jsp))/.*</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>Jersey Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>io.cnick.api</groupId>
    <artifactId>cnick-server</artifactId>
    <version>1.0</version>
    <name>cnick-api</name>
    <packaging>war</packaging>

    <build>
        <testSourceDirectory>src/test</testSourceDirectory>

        <finalName>cnick</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0</version>
                <inherited>true</inherited>
                <configuration>
                    <webXml>web/WEB-INF/web.xml</webXml>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.0.M2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.eclipse.jetty</groupId>
                                    <artifactId>jetty-runner</artifactId>
                                    <version>9.3.9.M1</version>
                                    <destFileName>jetty-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <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>
        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.7.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-inmemory</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19.3</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-mvc-jsp</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19</version>
        </dependency>

    </dependencies>

    <properties>
        <jersey.version>2.22</jersey.version>
        <jackson.version>2.7.4</jackson.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

索引控制器:

package io.cnick.api.controllers;

import com.sun.jersey.api.view.Viewable;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Created by nika on 5/3/17.
 */
@Path("/index")
public class IndexController {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable get() {
         return new Viewable("login");
    }

}

0 个答案:

没有答案