Search code examples
javaweb-servicesjax-rsjax-wsjersey-2.0

Unable to consume Jersey Webservice


I try to consume Jersey-2 webservice using postman. below my webservice

    @Path("UserService")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public class UserService {

        @POST
        @Path("/getUser")
        public Utilisateur getUser(@FormParam("identitifiant")String identifiant,@FormParam("password") String password)  {   

            UtilUtilisateur myUser = null;
            try {   
                myUser = requestUser(identifiant, password);

            } catch (Exception t) {

                log(t);
            } 
            return myUser;
        } 

Below my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Rest</display-name>

    <context-param>
        <param-name>flex.class.path</param-name>
        <param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>
    </context-param>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>


    <!-- WebService Servlet -->
    <servlet>
        <servlet-name>EmagREST</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>org.glassfish.jersey.config.property.packages</param-name>
            <param-value>com.ed.eMagMoney.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>EmagREST</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

I try to get user using postman below my URL with post parameters

http://localhost:8080/jersey/rest/UserService/getUser

But as return I have

<html>
    <head>
        <title>Apache Tomcat/6.0.16 - Rapport d'erreur</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>Etat HTTP 404 - Not Found</h1>
        <HR size="1" noshade="noshade">
            <p>
                <b>type</b> Rapport d'état
            </p>
            <p>
                <b>message</b>
                <u>Not Found</u>
            </p>
            <p>
                <b>description</b>
                <u>La ressource demandée (Not Found) n'est pas disponible.</u>
            </p>
            <HR size="1" noshade="noshade">
                <h3>Apache Tomcat/6.0.16</h3>
            </body>
        </html>

I don't understand why, How can I fix it ?


Solution

    1. Be sure to have all following dependencies in your project : -jersey-container-servlet-core -jersey-media-json-processing -jersey-media-json-jackson

    2. create a configuration class for your rest webservice, you can also load your package to be scan by jersey in those class, instead in web.xml

      import org.glassfish.jersey.jackson.JacksonFeature;
      import org.glassfish.jersey.server.ResourceConfig;
      
      public class Application extends ResourceConfig {
      
       public Application(){
       packages("com.package.of.your.resources").
       register(JacksonFeature.class);
       }
      }
      
    3. Don't forget to add the following annotation @XmlRootElement to your DAO class

    4. Other thing, in your service method,it's not necessary to add /(slash). Because the path is relative @Path('getUser') is enough.