mercredi 10 février 2016

Rest Web Service returning 404 Error

I am trying to learn simple restful web services. So, I looked up an example and followed it step by step but I am stuck with Error 404 when trying to deploy the project. Any idea why?

File: web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://ift.tt/ra1lAU" 
        xmlns="http://ift.tt/nSRXKP" 
        xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP" 
        id="WebApp_ID" version="3.0">
      <display-name>UserManagement</display-name>
      <servlet>
          <servlet-name>Jersey RESTful Application</servlet-name>
          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
             <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>com.tutorialspoint</param-value>
             </init-param>
          </servlet>
       <servlet-mapping>
       <servlet-name>Jersey RESTful Application</servlet-name>
          <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>  
    </web-app>

File: User.java:

    package com.tutorialspoint;

    import java.io.Serializable;

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "user")
    public class User implements Serializable {

        private static final long serialVersionUID = 1L;
        private int id;
        private String name;
        private String profession;

        public User(){}

        public User (int id, String name, String profession){
            this.id = id;
            this.name = name;
            this.profession = profession;
        }

        public int getID(){
            return id;
        }

        @XmlElement
        public void setId (int id){
            this.id = id;
        }

        public String getName(){
            return name;
        }

        @XmlElement
        public void setName(String name){
            this.name = name;
        }

        public String getProfession(){
            return profession;
        }

        @XmlElement
        public void setProfession(String profession){
            this.profession = profession;
        }
    }

File: UserDao.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers(){
        List<User> userList = null;
        try {
            File file = new File("Users.dat");
            if (!file.exists()) {
                User user = new User(1, "Mahesh", "Developer");
                userList = new ArrayList<User>();
                userList.add(user);
                saveUserList(userList);
            }
            else {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                userList = (List<User>) ois.readObject();
                ois.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        return userList;
    }

    private void saveUserList(List<User> userList) {
        // TODO Auto-generated method stub
        try {
            File file = new File ("User.dat");

            FileOutputStream fos = new FileOutputStream (file);
            ObjectOutputStream oos = new ObjectOutputStream (fos);
            oos.writeObject(userList);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

File: UserService.java

package com.tutorialspoint;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/UserService")
public class UserService {

    UserDao userDao = new UserDao();

    @GET
    @Path("/users")
    @Produces(MediaType.APPLICATION_XML)
    public List<User> getUsers() {
        return userDao.getAllUsers();
    }
}

Here's what I am getting back Error 404 screenshot




Aucun commentaire:

Enregistrer un commentaire