Internal Server Error - Tomcat & Jersey - java

I'm trying to go through the following tutorial, but I keep getting an error:
https://www.tutorialspoint.com/restful/restful_first_application.htm
When I try to acces the following link I get an HTTP Status 500 - Interval Server Error:
http://localhost:8080/NoteMandatory/rest/NoteService/notes
This is the error description I get:
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List.
The project folder:
Note.java:
package com.note;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "note")
public class Note implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String title;
private String text;
public Note(){
}
public Note(int id, String title, String text){
this.id = id;
this.title = title;
this.text = text;
}
//Getters
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getText() {
return text;
}
//Setters
#XmlElement
public void setId(int id) {
this.id = id;
}
#XmlElement
public void setTitle(String title) {
this.title = title;
}
#XmlElement
public void setText(String text) {
this.text = text;
}
}
NoteDataAccess.java:
public class NoteDataAccess {
public List<Note> getAllNotes(){
List<Note> noteList = null;
try {
File file = new File("Notes.dat");
if (!file.exists()) {
Note note = new Note(1, "Remember this", "Buy milk and do homework");
noteList = new ArrayList<Note>();
noteList.add(note);
saveNoteList(noteList);
}else{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
noteList = (List<Note>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("NoteDataAccess_getAllNotes Exception 1");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("NoteDataAccess_getAllNotes Exception 2");
}
return noteList;
}
private void saveNoteList(List<Note> noteList){
try {
File file = new File("Notes.dat");
FileOutputStream fos;
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(noteList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("NoteDataAccess_saveNoteList Exception 1");
} catch (IOException e) {
e.printStackTrace();
System.out.println("NoteDataAccess_saveNoteList Exception 2");
}
}
}
NoteService.java:
package com.note;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import org.apache.tomcat.jni.User;
#Path("/NoteService")
public class NoteService {
NoteDataAccess theNoteDataAccess = new NoteDataAccess();
#GET
#Path("/notes")
#Produces(MediaType.APPLICATION_XML)
public List<Note> getNotes(){
return theNoteDataAccess.getAllNotes();
}
}
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>NoteMandatory</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.note</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Jersey jar files:

I think the problem is that you are returning a list into an XML format. Jersey has trouble with this and you want to wrap it with a GenericEntity:
import javax.ws.rs.core.GenericEntity;
...
public class NoteService {
NoteDataAccess theNoteDataAccess = new NoteDataAccess();
#GET
#Path("/notes")
#Produces(MediaType.APPLICATION_XML)
public Response getNotes(){
List<Note> notes = theNoteDataAccess.getAllNotes();
GenericEntity<List<Note>> entity = new GenericEntity<List<Note>>(Lists.newArrayList(notes)) {};
return Response.ok().entity(entity).build();
}
See this SO Question.

Related

Unrecognized field class not marked as ignorable

Having the following:
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>avdisws</display-name>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>paqA.paqB.paqC.RestApplication</param-value>
</init-param>
</servlet>
</web-app>
Class Book.java:
#XmlRootElement(name = "book")
#XmlType(propOrder = {"title", "author", "price"})
public class Book {
private String title;
private String author;
private Double price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
Class BookService.java:
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
#Path("bookService")
#Produces(MediaType.APPLICATION_JSON + "; charset=ISO-8859-15")
#Consumes(MediaType.APPLICATION_JSON)
public class BookService {
#POST
#Path("test")
public Book getBook(#Context HttpServletRequest request, Book book) {
Book returnedBook = new Book();
returnedBook.setAuthor("Test Author");
returnedBook.setTitle("Test Title");
returnedBook.setPrice(99.99);
return returnedBook;
}
}
Class RestApplication:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public RestApplication() {
super();
singletons.add(new BookService());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
In my local JBoss AS 6 environment, I'am able to call http://localhost:8080/avdisws/bookService/test properly with this JSON:
{
"book":
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
}
BUT in the DESA environment, with Jboss EAP 7.1 I get:
Unrecognized field "book" (class paqA.paqB.paqC.Book), not marked as ignorable
If I do the call with next JSON it doesnt fails:
{
"title": "Some book",
"author": "Some author",
"price": 89.21
}
These are the libraries I am using:
javax.servlet-api-3.1.0.jar
jaxrs-api-3.0.10.Final.jar
resteasy-jaxrs-3.0.10.Final.jar
resteasy-client-3.0.10.Final.jar
resteasy-jaxb-provider-3.0.10.Final.jar
resteasy-jettison-provider-3.0.10.Final.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
commons-io-2.6.jar
commons-logging-1.2.jar
jettison-1.3.8.jar
Using #JsonIgnoreProperties(ignoreUnknown = true) does not solve the problem.
Thanks!
For me, this solve the problem:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
</exclusions>
</deployment>
</jboss-deployment-structure>

Cannot upload deployment On WildFly 11

I have a problem that is driving crazy the last few days.
I am trying to deploy a webapp in wildfly and I am getting the following error :
Cannot upload deployment: {"WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./webapp" => "com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. Caused by: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes."}}
This is my class
package com.stavros.ticketmanagement.rest;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import com.stavros.ticketmanagement.domain.Ticket;
#Path("/tickets")
public class TicketResource extends Application {
#GET
#Produces("application/xml")
public List<Ticket> getAllTickets(){
List<Ticket> results= new ArrayList<Ticket>();
results.add(new Ticket(1, "TestName", "Test time", 100, 0));
results.add(new Ticket(2, "TestName2", "Test time2", 102, 0));
return results;
}
}
This is my web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Configuration for JAX-RS -->
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.stavros.ticketmanagement.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
This is the Ticket class which I use in TicketResource
//Domain class. A class that represents the actual real word object (in this case a ticket)
package com.stavros.ticketmanagement.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;
#Entity //Make the class usable from JPA (We store data to db with this class)
#XmlRootElement
public class Ticket implements java.io.Serializable {
public Ticket() {
//required by JPA but not used
super();
}
#Id //Used from JPA as primary key
#GeneratedValue(strategy=GenerationType.AUTO)
private int ticketId;
private String eventName;
private String eventTime;
private int price;
private int isReserved;
#OneToMany(cascade=CascadeType.MERGE) // in the events of an un-persistence obj being found in the Nnotes collection jpa is free to automatically call persist on that obj
private Set<Note> notes; //this is used to keep a reference of the relative notes to each ticket. Its a collection (Set is better for DBs)Set: collection on obj with no particular order and no duplicate obj
// #ManyToOne //(cascade=CascadeType.PERSIST) // this should be manytoone
// private Set<User> users;
public Ticket(int ticketId, String eventName, String eventTime, int price, int isReserved) {
super();
this.notes = new HashSet<Note>();
this.ticketId = ticketId;
this.eventName = eventName;
this.eventTime = eventTime;
this.price = price;
this.isReserved = isReserved;
}
public int getTicketId() {
return ticketId;
}
public void setTicketId(int ticketId) {
this.ticketId = ticketId;
}
public String getEventTime() {
return eventTime;
}
public void setEventTime(String eventTime) {
this.eventTime = eventTime;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getIsReserved() {
return isReserved;
}
public void setIsReserved(int isReserved) {
this.isReserved = isReserved;
}
public Set<Note> getNotes() {
return notes;
}
public void setNotes(Set<Note> notes) {
this.notes = notes;
}
public String getEventName() {
return eventName;
}
//this is what the find*Ticket will return in TestHarness
public String toString() {
return "Ticket [ticketId=" + ticketId + ", eventName=" + eventName + ", eventTime=" + eventTime + ", price="+ price + ", isReserved=" + isReserved + "]";
}
//Regular methods, for example -10% to a ticket
public void setEventName(String newEventName) {
this.eventName = newEventName; //updates the eventName in the db
}
//bsn methods
//add a note
public void addNote(String newNoteText) {
Note newNote=new Note(newNoteText);
this.notes.add(newNote);
}
//add a user
// public void addUser(int userId, String userName, String password, String email, int accessLevel) {
// User newUser=new User(userId, userName, password, email, accessLevel);
// this.users.add(newUser);
// }
public Set<Note> getAllNotes() {
// TODO Auto-generated method stub
return this.notes;
}
}
I have tried every possible solution I found. The paths inside the web.xml are correct.
Here is a photo of my libs.
Thank you very much in advance for your help.!

Getting 404 error while accessing a restful web service

I have created a simple UserManagement restful web service. in which I have created 3 classes. The source codes are as following.
User class
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;
}
}
UserDao class
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 {
public List<User> getAllUsers(){
List<User> userList = null;
try {
File file = new File("Users.dat");
if (!file.exists()) {
User user = new User(1, "Mahesh", "Teacher");
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){
try {
File file = new File("Users.dat");
FileOutputStream fos;
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UserService class
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();
}
}
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>User Management</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>
Then I have created its war and deployed in Tomcat and start the Tomcat
But when I am accessing this restfull web service by hitting the url http://localhost:8080/UserManagement/rest/UserService/users
using Postman it gives 404 error.
Please some one help me out on this.
Try change servlet class with com.sun.jersey.spi.container.servlet.ServletContainer .

Rest Webservice returning http status 404

I am using Apache Tomcat 6 and the Jersey 2.0 libraries. I am running this service on 8088 port. While trying to access it, getting 404 not found error. This is my service:
User Class
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;
}
}
UserDao Class
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 {
public List<User> getAllUsers(int id){
List<User> userList = null;
try {
User user = null;
if(id == 1){
user = new User(1, "Mahesh", "Teacher");
}
if(id == 2){
user = new User(2, "Manish", "Doctor");
}
userList = new ArrayList<User>();
userList.add(user);
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
UserService class
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
#GET
#Path("{id}")
#Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(#PathParam("id") int id){
return userDao.getAllUsers(id);
}
}
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>UserManagement</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My project Stucture is like this
Getting this error

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://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>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
Besides the possibility that your application is not even deployed (also 404):
The path you configured your rest service to answer is
<Your application context>/rest/UserService/users
<Your application context> seems to be /UserManagement.
/rest is configured in your web.xml in the jersey servlet mapping
/UserService is configured in your UserService.java file in the #Path annotation
/users is configured in UserService.java file on the method's #Path annotation. This should be #Path("users").
Your screenshot shows you are calling /UserManagement, which may be the context of your application, but is not the path to your service.
You can change you web.xml file. There is some problem with webxml file. Use this web.xml file use below code.
<?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>User Management</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>

Categories