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.!
Related
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("abc#gmail.com");
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:
Could you please suggest a way of solving this issue?
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.
Your URL should be: localhost:8082/UserAPI/rest/AddUserApi/users
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
I'm getting this error: MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item.
What I want to do is let my restful service method return a custom object, in this case object: Item. But im getting this error and in the browser error 500.
Im using JAX-RS version 2.25.1 libraries in netbeans
I know there are allot of questions like this but i could not find any solution that worked exactly for me.
Thanks in advance!
WEB.XML
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>examples.rest.school</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>SalesService</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Resources Service class:
package SalesService;
import java.util.ArrayList;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import DB.DBConnecter;
import javax.ws.rs.PathParam;
import java.util.List;
import javax.ws.rs.Consumes;
#Path("Sales")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class SalesResources {
private final DBConnecter dbConnector;
public SalesResources() {
dbConnector = new DBConnecter();
}
#GET
#Path("session/{sessionID}")
#Produces(MediaType.APPLICATION_JSON)
public Item GetItem(#PathParam("sessionID") String sessionID) {
List<Item> items = new ArrayList<>();
if(dbConnector.CheckSessionID(sessionID)){
items = dbConnector.GetItems();
return items.get(0);
}
return new Item();
}
}
Item class:
package SalesService;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Item {
private int id;
private String name;
private int quantity;
private double price;
private String type;
public Item(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void AddQuantity() {
this.quantity = quantity + 1;
}
public void RemoveQuantity() {
this.quantity = quantity - 1;
}
public Item(int id, String name, int quantity, double price, String type) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
this.type = type;
}
}
StackTrace
04-Mar-2017 21:03:06.273 SEVERE [http-nio-8080-exec-1] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item.
This was resolved adding
org.codehaus.jackson.jaxrs
In web.xml like this
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>examples.rest.school</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>SalesService,org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
After adding 4 jackson .JAR files which are these
And also adding them in libraries folder (reference)
Hello all i have sample restful service below with jersey 2
MODEL Class
#XmlRootElement(name = "book")
#XmlType(propOrder = { "id", "name", "author", "price" })
public class Book {
private String id;
private String name;
private String price;
private String author;
public Book() {
}
public Book(String id, String name, String price, String author) {
super();
this.id = id;
this.name = name;
this.price = price;
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
My DAOImpl
public class BooksImpl{
static Connection conn = null;
static Statement stmt;
ResultSet rs;
public List<Book> getAllBooks() throws SQLException, ClassNotFoundException, FileNotFoundException {
getConnection();
List<Book> arrBook = new ArrayList<Book>();
rs = stmt.executeQuery(GET_ALL_BOOKS);
while (rs.next()) {
arrBook.add(new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
rs.close();
stmt.close();
conn.close();
return arrBook;
}
Service
#Path("/library")
#Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
#Consumes({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
public class BookServiceImpl implements BookService {
private BooksImpl booksImpl = new BooksImpl();
#GET
public Response getBooks(#QueryParam("format") String format) throws SQLException, ClassNotFoundException, FileNotFoundException {
return Response.status(Status.OK).entity(new GenericEntity<List<Book>>(booksImpl.getAllBooks()) {
}).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8")
.status(Status.OK).build();
}
So finally i make a get request say in a postman like ::
http://localhost:8080/BooksJAXRS/library
It's good and i can get the reponse by getting all the books from DB.
my web.xml
<?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>BooksJAXRS</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>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-servlet</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.library.books</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My requriment is for different http methods we have different url end point paths for example
GET : http://localhost:8080/BooksJAXRS/library
POST : http://localhost:8080/BooksJAXRS/library
DELETE : http://localhost:8080/BooksJAXRS/library/1
PUT : http://localhost:8080/BooksJAXRS/library/1
So i make a props file
GET=http://localhost:8080/BooksJAXRS/library
POST=http://localhost:8080/BooksJAXRS/library
DELETE= http://localhost:8080/BooksJAXRS/library/1
PUT= http://localhost:8080/BooksJAXRS/library/1
Now i want to capture that restful url and pass it through props file and route it accordingly depending on the method and url
Thank you
Mark
You could create 2 mappings on 2 different methods that call the same private method like this:
#Path("/")
...
public class BookServiceImpl implements BookService {
#Path("/")
#GET
public Response getBooksByRoot(#QueryParam("format") String format) {
return getBooks(format);
}
#Path("/library")
#GET
public Response getBooksByLibrary(#QueryParam("format") String format) {
return getBooks(format);
}
private Response getBooks(String format) {
...
}
#Path("/library")
#POST
public Response createBook(#QueryParam("name") String name) {
...
}
Response Update:
It seems that you want a dynamic mapping, so you need to use a Path with a regular expression.
#Path("/")
...
public class BookServiceImpl implements BookService {
#Path("/{parameter:.*}")
#GET
public Response getBooks(#PathParam("parameter") String parameter) {
if (properties.getProperty("GET").endsWith(parameter) {
// code to get the books here
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
WARNING: The code snippet above is only meant to show the idea, it is not meant to be perfect. For example testing with endsWith could not be enough according to your context.
When using standard web descriptor (url-pattern /*) and annotated end-points (#Path /library) there is not so much that can be done for dynamic (at runtime) routing at this points. If you want or need some dynamic routes, one option put the "app" into a new context like:
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
And add some standard Servlet to forward to your desired end-points:
<servlet>
<servlet-name>route</servlet-name>
<servlet-class>my.package.Route</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>route</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
my.package.Route will be get hit on any other than /app/* request and you can do your routing to forward the request anywhere.
I now use is jersey2.13 and spring4.X, and can't get jersey bean validation to work.
The following is my configuration and code snippets:
web.xml
<servlet>
<!-- jersey ServletContainer -->
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- jersey resource packages-->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.wordnik.swagger.jaxrs.json,
com.ghca.easyview.server.api.resource
</param-value>
</init-param>
<!-- jersey provider classnames-->
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.wadl.disableWadl</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.ghca.easyview.server.api.context.JerseyApplication</param-value>
</init-param>
<!-- jersey beanValidation -->
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/se/rest/*</url-pattern>
</servlet-mapping>
<!-- jersey swagger UI -->
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://10.143.132.99:4000/api/se/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Bootstrap</servlet-name>
<servlet-class>com.ghca.easyview.server.api.plugin.swagger.Bootstrap2</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
JerseyApplication.java:
package com.ghca.easyview.server.api.context;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.ContextResolver;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.validation.ValidationConfig;
import org.glassfish.jersey.server.validation.internal.InjectingConstraintValidatorFactory;
/**
* ContactCard application configuration
* JAX-RS application
*/
public class JerseyApplication extends ResourceConfig{
public JerseyApplication(){
property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
property(ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
property(ServerProperties.BV_FEATURE_DISABLE, true);
// Resource Package Address
packages("com.ghca.easyview.server.api.resource");
//Validation.
register(ValidationConfigurationContextResolver.class);
}
/**
*/
public static class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig>{
#Context
private ResourceContext resourceContext;
#Override
public ValidationConfig getContext(final Class<?> type) {
return new ValidationConfig().constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class));
}
}
}
PersonResource.java:
package com.ghca.easyview.server.api.resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
import com.ghca.easyview.server.api.request.validation.model.Person;
import com.wordnik.swagger.annotations.Api;
#Component
#Path("/person")
#Api(value = "/person", description = "person", position = 1)
#Produces({ "application/json", "application/xml" })
public class PersonResource {
#POST
public Response create(#Valid #NotNull Person person) {
System.out.println(person);
return Response.ok().build();
}
}
Person.java:
package com.ghca.easyview.server.api.request.validation.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
public class Person {
#NotNull
private Integer id;
#NotNull
#Length(min = 2, max = 20)
private String name;
#Email(message = "{contact.wrong.email}", regexp = "[a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}")
private String email;
#Pattern(message = "{contact.wrong.phone}", regexp = "[0-9]{3,9}")
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email
+ ", phone=" + phone + "]";
}
}
Not sure why you are disabling everything, but after testing, removing the disable properties makes it work
//property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
//property(ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
//property(ServerProperties.BV_FEATURE_DISABLE, true);
Those properties are false by default, which allows auto-discovery. You are disabling it.