Java-ee REST server with IntelliJ and Tomcat

前端 未结 1 1435
情歌与酒
情歌与酒 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

    
        javax
        javaee-api
        7.0
    
    

    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

    
        org.glassfish.jersey.containers
        jersey-container-servlet
        2.17
    
    

    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

    
        org.glassfish.jersey.media
        jersey-media-json-processing
        2.17
    
    

    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

    
        org.glassfish.jersey.media
        jersey-media-json-jackson
        2.17
    
    

    0 讨论(0)
提交回复
热议问题