Java-ee REST server with IntelliJ and Tomcat

前端 未结 1 1449
情歌与酒
情歌与酒 2021-01-22 21:29

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

I could develop a servlet

@WebServ         


        
相关标签:
1条回答
  • 2021-01-22 22:23

    "Instead of Glassfish, I use Tomcat."

    Look at this

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

    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.

    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>
    

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

    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>
    

    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>
    
    0 讨论(0)
提交回复
热议问题