如何使用Jersey和Mustache模板引擎创建RESTful应用程序?

时间:2021-01-30 14:30:55

Edit My initial problem was that I was not using the correct version of jersey. So, I made changes and kept the previous question for future reference. However, if you are using Jersey 2.x, please go to the Update header in this question.

编辑我最初的问题是我没有使用正确版本的球衣。所以,我做了更改并保留了上一个问题以供将来参考。但是,如果您使用的是Jersey 2.x,请转到此问题的“更新”标题。

I am trying to write a simple web application using Jersey and Mustache, running on Jetty. For building the project, I am using Gradle.

我正在尝试使用在Jetty上运行的Jersey和Mustache编写一个简单的Web应用程序。为了构建项目,我正在使用Gradle。

Here is my buildfile.

这是我的构建文件。

apply plugin: 'jetty'

repositories {
    mavenCentral();
}

dependencies {
   providedCompile 'javax.servlet:servlet-api:2.5'
   runtime 'javax.servlet:jstl:1.1.2'

   compile 'com.sun.jersey:jersey-core:1.17.1'
   compile 'com.sun.jersey:jersey-server:1.17.1'            
   compile 'com.sun.jersey:jersey-servlet:1.17.1'

   compile 'org.glassfish.jersey.ext:jersey-mvc-mustache:2.3.1'
}

httpPort = 8080
stopPort = 9080
stopKey = "stopKey"

and my web.xml

和我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>XPoViz</display-name>

    <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.webapp</param-value>
        </init-param>

        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.mvc.mustache.MustacheMvcFeature</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

and I have a simple controller class

我有一个简单的控制器类

package com.example.webapp;

@Path("login")
public class Login {
    @Context
    private SecurityContext context;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Viewable get(){
        return new Viewable("index", "FOO");
    }
}

In the same package (com.example.webapp) I have created a mustache file called index.mustache which is a simple HTML page

在同一个包(com.example.webapp)中,我创建了一个名为index.mustache的小胡子文件,它是一个简单的HTML页面

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Simple mustache test</title>
  </head>
  <body>
    <h1>This is mustacheee</h1>
  </body>
</html>

When I do gradle jettyRun it starts the server. However, when I try to access the login path, it raises 500 error and I see the following error on my console

当我做gradle jettyRun时它会启动服务器。但是,当我尝试访问登录路径时,它会引发500错误,我在控制台上看到以下错误

A message body writer for Java class org.glassfish.jersey.server.mvc.Viewable, and Java type class org.glassfish.jersey.server.mvc.Viewable, and MIME media type application/xml was not found

未找到Java类org.glassfish.jersey.server.mvc.Viewable的消息正文编写器和Java类型类org.glassfish.jersey.server.mvc.Viewable和MIME媒体类型application / xml

As I am new to Java EE web development and Jersey and Jersey-MVC, I am unable to figure out what I am doing wrong and how to fix it.

由于我是Java EE Web开发和Jersey和Jersey-MVC的新手,我无法弄清楚我做错了什么以及如何解决它。

Update After fixing the jersey version

更新修复球衣版本后

I was using old version of Jersey. So, now the build.gradle updated

我使用旧版泽西岛。所以,现在build.gradle已更新

apply plugin: 'jetty'


jettyRun {
    reload = "automatic"
    scanIntervalSeconds = 1
}

repositories {
    mavenCentral();
}

dependencies {      
    compile 'org.glassfish.jersey.core:jersey-server:2.17'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.17'

    compile 'org.glassfish.jersey.ext:jersey-mvc-jsp:2.17'
    compile 'org.glassfish.jersey.ext:jersey-mvc-mustache:2.17'
}

httpPort = 8080
stopPort = 9080
stopKey = "stopKey"

Also, I decided to use ResourceConfig. So my web.xml changes to

此外,我决定使用ResourceConfig。所以我的web.xml改为

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>XPoViz</display-name>

    <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.webapp.MyApplication</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

Here is MyApplication

这是MyApplication

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Add a package used to scan for components.
        packages(this.getClass().getPackage().getName());
        register(MustacheMvcFeature.class);
    }
}

Now, I have a class/resource that should serve using Mustache. It is :

现在,我有一个应该使用Mustache服务的类/资源。它是 :

@Path("/mustache")
public class MustacheTest {

    @GET
    @Template(name = "/index.mustache")
    public String get(){
        return "foo";
    }
}

I also added the index.mustache file in the WEB-INF directory. However, when I access http://localhost:8080/Backend/mustache, it gives me a 404 (Note: when I run gradle jettyRunWar, the web app is deployed at localhost:8080/Backend)

我还在WEB-INF目录中添加了index.mustache文件。但是,当我访问http:// localhost:8080 / Backend / mustache时,它会给我一个404(注意:当我运行gradle jettyRunWar时,Web应用程序部署在localhost:8080 / Backend)

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


Problem is this

问题是这个

compile 'org.glassfish.jersey.ext:jersey-mvc-mustache:2.3.1'

That's a Jersey 2.x module. You are using Jersey 1.x. These two major versions are incompatible. Registering the feature does nothing.

这是泽西岛2.x模块。您正在使用Jersey 1.x.这两个主要版本不兼容。注册该功能不会做任何事情。

Now Jersey 1.x comes with the MVC support without any extension modules. The dependencies you have are sufficient. The Viewable class though is actually

现在Jersey 1.x带有MVC支持,没有任何扩展模块。您拥有的依赖关系就足够了。实际上是Viewable类

com.sun.jersey.api.view.Viewable

You're using the Jersey 2.x org.glassfish one.

你正在使用Jersey 2.x org.glassfish。

But this doesn't solve the problem about using Mustache. Currently from I can tell by looking at the different extension modules, I don't see any support for Mustache, only Freemarker. I don't know, but you may need to switch to Jersey 2.x if you want to use the Mustache support, unless there is a 1.x implementation somewhere out there I'm unaware of.

但这并没有解决使用Mustache的问题。目前我可以通过查看不同的扩展模块来判断,我看不到任何对Mustache的支持,只有Freemarker。我不知道,但如果你想使用Mustache支持,你可能需要切换到Jersey 2.x,除非在那里有一个1.x实现,我不知道。

#1


Problem is this

问题是这个

compile 'org.glassfish.jersey.ext:jersey-mvc-mustache:2.3.1'

That's a Jersey 2.x module. You are using Jersey 1.x. These two major versions are incompatible. Registering the feature does nothing.

这是泽西岛2.x模块。您正在使用Jersey 1.x.这两个主要版本不兼容。注册该功能不会做任何事情。

Now Jersey 1.x comes with the MVC support without any extension modules. The dependencies you have are sufficient. The Viewable class though is actually

现在Jersey 1.x带有MVC支持,没有任何扩展模块。您拥有的依赖关系就足够了。实际上是Viewable类

com.sun.jersey.api.view.Viewable

You're using the Jersey 2.x org.glassfish one.

你正在使用Jersey 2.x org.glassfish。

But this doesn't solve the problem about using Mustache. Currently from I can tell by looking at the different extension modules, I don't see any support for Mustache, only Freemarker. I don't know, but you may need to switch to Jersey 2.x if you want to use the Mustache support, unless there is a 1.x implementation somewhere out there I'm unaware of.

但这并没有解决使用Mustache的问题。目前我可以通过查看不同的扩展模块来判断,我看不到任何对Mustache的支持,只有Freemarker。我不知道,但如果你想使用Mustache支持,你可能需要切换到Jersey 2.x,除非在那里有一个1.x实现,我不知道。