2

I am trying to build a simple Restful api which returns user details.

I have refered this link.

Following are my classes:

API Service class:

package com.demoapp; 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; import org.omg.Messaging.SyncScopeHelper; @Path("/AddUserApi") public class AddUserApi { @GET @Path("/users") @Produces(MediaType.APPLICATION_XML) public List<User> getUsers(){ return UserUtil.getUserList(); } } 

Dao Class:

package com.demoapp; import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; String name=""; String u_name=""; String password=""; String email=""; String user_type=""; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getU_name() { return u_name; } public void setU_name(String u_name) { this.u_name = u_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUser_type() { return user_type; } public void setUser_type(String user_type) { this.user_type = user_type; } } 

Utils class:

package com.demoapp; import java.util.List; public class UserUtil { public static String mysql_ip="jdbc:mysql://10.119.32.86/"; public static String metadata_database="haas"; public static String mysql_username="root"; public static String mysql_password=""; public static List<User> getUserList(){ List<User> users=new ArrayList<User>(); User user=new User(); user.setName("abc"); user.setU_name("ab_c"); user.setPassword("1234"); user.setUser_type("normal"); user.setEmail("[email protected]"); users.add(user); return users; } } 

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>UserAPI</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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.demoapp</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey RESTful Application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> 

I have downloaded the jersey jar from the site and extracted 3 folders and added those jars in build path.

I have done everything according to the tutorial. Still its showing this error:

error Could you please suggest a way of solving this issue?

2 Answers 2

2

Provided the server is up and running and the application is deployed without errors, your endpoint should be available in the following URL:

http://[host]:[port]/[context]/rest/AddUserApi/users 

Where:

  • [context] is the name of your WAR file (without the .war extension).
  • /rest comes from the web.xml: <url-pattern>/rest/*</url-pattern>.
  • /AddUserApi comes from the @Path annotation on the AddUserApi class.
  • /users comes from @Path annotation on the getUsers() method.
Sign up to request clarification or add additional context in comments.

4 Comments

i am running the project on server by clicking on run as server....so do you mean i have to make war and deploy it on a server and then use the link?
@satishsilveri No, you don't need to manually create a WAR file because your IDE is doing it for you (actually, it's very likely that your application is being deployed as an exploded WAR folder, but it doesn't matter). For a dynamic web project, most IDEs use the project name as [context] by default.
Mazzochi Molin so i done that only running from IDE and i am assuming it should create this link localhost:8082/UserAPI/rest/AddUserApi/users automatically...correct me if i am wrong.
@satishsilveri I don't know what you mean with create this link ... automatically. But if the server is running on the 8082 port and the application is deployed without errors on the UserAPI context, you should be able to perform a GET request to http://localhost:8082/UserAPI/rest/AddUserApi/users and get the desired output.
0

Your URL should be: localhost:8082/UserAPI/rest/AddUserApi/users

2 Comments

Where did you get the port 8082 and the context UserAPI?
Are you sure the opened port is 8082 instead of 8080?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.