jetty + jersey + FreeMaker,找不到媒体类型= text / html的MessageBodyWriter

时间:2017-05-27 21:29:29

标签: java jersey jetty freemarker

我在尝试提供FreeMaker'ftl'文件时遇到此错误。你可以帮忙吗? 我实际上找不到这个解决方案。

这里是id日志输出:

May 28, 2017 12:20:07 AM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: The (sub)resource method getMain in com.shay.tasks.server.WebHandler contains empty path annotation.

May 28, 2017 12:20:07 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/html, type=class org.glassfish.jersey.server.mvc.Viewable, genericType=class org.glassfish.jersey.server.mvc.Viewable.

这是我的等级依赖:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies  {
    compile group: 'com.google.code.gson'           , name: 'gson'                       , version: '2.8.0'
    compile group: 'log4j'                          , name: 'log4j'                      , version: '1.2.17'
    compile group: 'commons-io'                     , name: 'commons-io'                 , version: '2.5'   //used for force made dirs
    compile group: 'org.apache.commons'             , name: 'commons-lang3'              , version: '3.5'   //String Utils etc..

    //Command line options
    compile group: 'com.beust'                      , name: 'jcommander'                 , version: '1.69'

    //Web Server
    compile group: 'org.glassfish.jersey.core'      , name: 'jersey-server'              , version: '2.25.1'
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet'   , version: '2.25.1'
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-jetty-http', version: '2.25.1'
    compile group: 'org.glassfish.jersey.ext'       , name: 'jersey-mvc'                 , version: '2.25.1'
    compile group: 'org.glassfish.jersey.ext'       , name: 'jersey-mvc-freemaker'       , version: '2.25.1'
    compile group: 'org.eclipse.jetty'              , name: 'jetty-server'               , version: '9.4.5.v20170502'
    compile group: 'org.eclipse.jetty'              , name: 'jetty-servlet'              , version: '9.4.5.v20170502'

    //Template engine
    compile group: 'org.freemarker'                 , name: 'freemarker'                 , version: '2.3.26-incubating'
}

这是我的控制器

import org.glassfish.jersey.server.mvc.Viewable;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shaytessler on 5/19/17.
 */
@Path("pages")
public class WebHandler {

    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public Viewable getMain() {
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", "Pavel");
        final List<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("item2");
        list.add("item3");
        map.put("items", list);
        return new Viewable("/hello.ftl", map);
    }
}

和模板:hello.ftl

<html>
    <head>
      <title>Welcome!</title>
    </head>

    <body>
      <h1>Welcome ${user}!</h1>
      <p>items:<br />
            <#list items as item>
                ${item}<br />
            </#list>
      </p>
    </body>
</html>
    ServletContainer container = new ServletContainer(new ResourceConfig().register(FreemarkerMvcFeature.class));
    ServletHolder jerseyServlet = new ServletHolder(container);
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", WebHandler.class.getCanonicalName());


    try {
        jettyServer.start();
        jettyServer.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        jettyServer.destroy();
    }

感谢您的帮助

0 个答案:

没有答案