带有IntelliJ和Tomcat的Java-ee REST服务器 [英] Java-ee REST server with IntelliJ and Tomcat

查看:139
本文介绍了带有IntelliJ和Tomcat的Java-ee REST服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java-ee在这个之后实现REST服务器API教程。我使用Tomcat而不是Glassfish。

I'm trying to implement a REST Server API using Java-ee following this tutorial. Instead of Glassfish, I use Tomcat.

我可以开发一个servlet

I could develop a servlet

@WebServlet(name = "hello", urlPatterns = "/")
public class HelloWorld extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("=)");
    }
}

并加入 http:// localhost:9080 / 我可以看到笑脸。但是当我尝试访问api路径时( http:// localhost:9080 / api / recommended / all )我也得到了面子。如果我删除servlet类,我会收到404错误。我想我需要其他东西来自动构建api,但我不知道是什么。

And join http://localhost:9080/ I can see the smiling face. But when I try to access to the api path (http://localhost:9080/api/recommend/all) I also get the face. If I remove the servlet class, I get a 404 error. I suppose I need something else to build automatically the api but I don't know what.

有人能告诉我遗漏了什么吗?我该怎么办?

Could someone tell my what is missing? What should I do?

更新:
在Intellij的Java Enterprise View中我看到:

Update: In Intellij's Java Enterprise View I see:

Web > HelloWorld
RESTful WS > recommend > all

这些是我的api课程:

These are my api classes:

@ApplicationPath("/api")
public class REST_Config extends Application {
}

具体方法

@Path("recommend")
public class RecommenderController {

    @Path("/all")
    @GET
    @Produces("application/json")
    public JsonArray getAll(){
        JsonArrayBuilder builder = Json.createArrayBuilder();

        builder.add(Json.createObjectBuilder().add("1", "2.5"));

        return builder.build();
    }
}

和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>TestREST</groupId>
    <artifactId>TestREST</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>TestREST</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
            </plugin>

        </plugins>
    </build>
</project>


推荐答案


而不是Glassfish ,我使用Tomcat。

"Instead of Glassfish, I use Tomcat."

看看这个

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

这只不过是EE规范的一堆接口。没有实施。 Java EE服务器将具有实现。 Tomcat不是EE服务器。它将肯定实现的EE规范的唯一部分是Servlet规范。您正在尝试使用JAX-RS规范,默认情况下Tomcat没有实现。所以你需要添加那个实现。

This is nothing more than basically a bunch of interfaces for the EE spec. There is no implementation. Java EE servers will have the implementation. Tomcat is not an EE server. The only part of the EE spec it will definitely implements is the Servlet Specification. You are trying to work with the JAX-RS spec, where Tomcat for sure by default does not have an implementation for. So you need to add that implementation.

开始使用的最简单的IMO是Jersey。您可以简单地添加此依赖项

The easiest IMO to get started with, is Jersey. You can simple add this dependency

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

它会启动并运行。保持 Jersey用户指南的便利。它将被使用。

And it will get you up and running. Keep the Jersey User Guide handy. It will come in use.

另外我不知道 JsonArray 是什么,但是当你发生什么时会发生什么运行这是你会得到一些类似于没有为JsonArray和媒体类型应用程序/ json找到MessageBodyWriter的错误。你需要提供者。如果您要使用Java EE JSONP API,那么您应该添加此提供商

Also I don't know what JsonArray is, but what will happen when you run this is you will get some error similar to "No MessageBodyWriter found for JsonArray and media type application/json". You need to provider. If you are going to use the Java EE JSONP API, then you should add the this provider

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.17</version>
</dependency>

当你开始大量使用JSON时,你会发现这个API难以维护。我建议使用杰克逊。如果您还不知道,我建议您学习它。它提供简单的POJO到JSON映射。对于Jackson,您可以添加此依赖关系

As you get to working alot with JSON, you wil find this API to be difficult to maintain. I'd recommend using Jackson. If you don't already know it, I'd suggest learning it. It offers simple POJO to JSON mapping. For Jackson, you can add this dependency

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

这篇关于带有IntelliJ和Tomcat的Java-ee REST服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆