404 error with Jersey REST Service on Tomcat

后端 未结 1 836
野性不改
野性不改 2021-01-14 22:16

I have looked at all the answers available on this topic, either I am facing a completely different problem or I am missing something big time.

Service Class:

<
相关标签:
1条回答
  • 2021-01-14 22:55

    "I am using Jersey 2.17"

    This does not exist in 2.17

    com.sun.jersey.spi.container.servlet.ServletContainer
    

    I'm surprised you're not getting a class not found exception. Which means you are probably mixing versions. Or maybe you're getting an exception an not telling us. In any case, the correct ServletContainer should be

    org.glassfish.jersey.servlet.ServletContainer
    

    Next what you should do, if you have any, is get rid of everything (jar) whose packages begin with com.sun. These are Jersey 1 jars. In Jersey two, the package naming pattern changed to org.glassfish.xxx If you want to make life easy, use Maven, and simply add just one dependency to whole project, and it will pull in all the rest.

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

    Also, this doesn't exist in Jersey two either

    jersey.api.json.POJOMappingFeature
    

    In Jersey 2, simply add this Maven depedency, and life will be good.

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

    If you're not using Maven, download the RI bundle here. Open all the folders and add every jar to your project. This is for core support.

    For JSON support, download this, as well as all of these. You can search the same site for them. doing this should work with no extra configuration. Alternatively, you can just download only the ones in the second link, then add the package in the web.xml, as seen in the link.

    But just to get it working, since your code doesn't produce or consume any JSON, you can simply get the core running first, then once it starts working correctly, you can work on the JSON support.

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