Maven 2 未找到媒体类型为text/html的Jersey MessageBodyWriter

Maven 2 未找到媒体类型为text/html的Jersey MessageBodyWriter,maven-2,thymeleaf,jersey-2.0,tomcat8,Maven 2,Thymeleaf,Jersey 2.0,Tomcat8,我将使用“Thymeleaf”显示html,但给出了以下错误。 使用的服务器是tomcat8。 我的代码看起来像 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

我将使用“Thymeleaf”显示html,但给出了以下错误。 使用的服务器是tomcat8。 我的代码看起来像

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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.abc.def</groupId>
<artifactId>jersey_de</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jersey_de</name>

<build>
    <finalName>jersey_de</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- my setting -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.connectors</groupId>
        <artifactId>jersey-apache-connector</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc</artifactId>
        <version>2.21</version>
    </dependency>
    <!-- 
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc-jsp</artifactId>
        <version>2.21</version>
    </dependency>
     -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
百里香电子视觉处理器

@Provider
public class ThymeleafViewProcessor implements TemplateProcessor<String> {

    @Context
    private HttpServletRequest request;

    @Context
    private HttpServletResponse response;

    @Context
    private ServletContext servletContext;

    private final TemplateEngine templateEngine;

    /**
     * コンストラクタ
     */
    public ThymeleafViewProcessor() {

        TemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCacheTTLMs(3600000L);

        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);
        System.out.println("ThymeleafViewProcessor end");
    }

    /**
     * 参照させるファイル名を解決する。
     */
    @Override
    public String resolve(String name, MediaType mediaType) {

        return name;
    }

    @Override
    public void writeTo(String templateReference, Viewable viewable, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream out) throws IOException {

        WebContext context = new WebContext(request, response, servletContext);

        //HTML内でモデルオブジェクトを使用できるようにしている。
        //ここはモデル名をそのまま使用できるようにした方が良い。
        context.setVariable("item", viewable.getModel());

        Writer writer = new OutputStreamWriter(out);
        templateEngine.process(templateReference, context, writer);

        writer.flush();
    }
}
事情解决了。 我修改的要点如下

web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.abc.def.jersey_de.MyApplication</param-value>
    </init-param>

javax.ws.rs.Application
com.abc.def.jersey_de.MyApplication
聚甲醛


爪哇
javaeewebapi
7
org.apache.tomcat
TomcatServletAPI
8.0.15
org.glassfish.jersey
针织面料
${jersey.version}
聚甲醛
进口
org.glassfish.jersey.core
泽西服务器
${jersey.version}
org.glassfish.jersey.containers
jersey容器servlet核心
${jersey.version}
org.glassfish.jersey.media
泽西媒体公司
${jersey.version}
org.glassfish.jersey.ext
泽西mvc
${jersey.version}
org.thymeleaf
百里香
2.1.4.1发布
2.21
UTF-8

我理解。它是原创的。我在问题上加了一条线。
@ApplicationPath("/service")
public class ApplicationSetting extends ResourceConfig {

    public ApplicationSetting() {

        packages(this.getClass().getPackage().getName());

        register(ThymeleafViewProcessor.class);
        register(MvcFeature.class);
        register(LoggingFilter.class);
        System.out.println("ApplicationSetting ");
    }

}
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.abc.def.jersey_de.MyApplication</param-value>
    </init-param>
<dependencies>
    <dependency>
        <groupId>javax</groupId><!-- 動きが変わらないので不要? -->
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>8.0.15</version>
    </dependency>
    <dependency><!-- 不要? -->
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>${jersey.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
</dependencies>
<properties>
    <jersey.version>2.21</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>