Tears ran, tune for two days finally tune into. Let's talk about the problems.
Jersey 1.x and 2.x projects in different config, and Jersey 2.x Com.sun.jersey path to the Org.glassfish.jersey, this is the largest pit. The author did not notice at first, open 1.x tutorial with 2.x package, even if later found this problem, but because the 1.x sink too deep and change not back.
Students will say, the use of 1.x package is not good. The fact is not so simple, jersey1.1 and 1.9 configuration is also different, and the official now only provide 1.9 download, download down there are more than n jar package, OK, follow the tutorial, results Tomcat jumped. A closer look at the Tomcat version of the tutorial is 6.0, and I use 8.0.
This is really tears, the variable all the way up, let me really day dog mood have, two days tune down really tears run, fortunately finally found a reliable article, now come to concrete say method
Environment
1. Asm-3.1.jar
2. Gson-2.3.1.jar
3. Jersey-bundle-1.17.1.jar
4. Mysql-connector-java-5.1.38-bin.jar
5. Tomcat 8.0
In fact, only 3 of the real deploy WS
Project structure
2. Dao&dto
Package Com.qad.yab.dao;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.SQLException; public class Cvcjdbc {private Connection conn;public Connection Getconn () throws ClassNotFoundException, SQLException {if (NULL! = conn) {return conn;} else {class.forname ("Com.mysql.jdbc.Driver"); conn = Drivermanager.getconnection ("jdbc:mysql://localhost:3306/ Test1 "," CVC "," ""); SYSTEM.OUT.PRINTLN ("Connect Success"); return conn;}}
Package Com.qad.yab.dao;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet; Import Java.util.arraylist;import Com.qad.yab.dto.user;public class Cvcuserdao {private Connection Conn;public Arraylist<user> Getusers () throws Exception {conn = new Cvcjdbc (). Getconn (); String sql = "SELECT * from user;"; PreparedStatement stmt = conn.preparestatement (sql); ResultSet result = Stmt.executequery (); arraylist<user> datas = new arraylist<user> (); while (Result.next ()) {User data = new User ();d Ata.setid ( Result.getint ("id"));d Ata.setloginid ("LoginID");d Ata.setname (result.getstring ("name")); Data.setemail (result.getstring ("email"));d Atas.add (data);} Stmt.close (); return datas;}}
Package Com.qad.yab.dto;public class User {private int id; Private String LoginID; private String name; Private String Email; Private Boolean active; /** * Default constructor to create new user */public user () {super (); }/** * Create new user with user name * * @param name * The login ID */public user ( String LoginID) {super (); This.loginid = LoginID; }/** * @return the ID */public int getId () {return id; }/** * @param ID * The ID to set */public void setId (int id) {this.id = ID; }/** * @return the LoginID * */Public String GetLoginId () {return loginID; }/** * @param LoginID * The LoginID to set */public void Setloginid (String loginID) { This.loginid = LoginID; }/** * @return the name */public String GetName () {return name; } /** * @param name * The name to set */public void SetName (String name) {this.name = Name }/** * @return The email * * * public String Getemail () {return email; }/** * @param email * The email to set */public void Setemail (String email) {THIS.E mail = email; }/** * @return the active */public boolean isActive () {return active; }/** * @param active * The active to set */public void setActive (Boolean active) {T His.active = active; }}
4. WebService
Package Com.qad.yab.ws;import Java.util.arraylist;import Javax.ws.rs.get;import javax.ws.rs.path;import Javax.ws.rs.produces;import Com.google.gson.gson;import Com.qad.yab.dao.cvcuserdao;import com.qad.yab.dto.User;@ Path ("/webservice") public class UserService {@GET @path ("/cvcuser") @Produces ("Application/json") public String User () Throws Exception {String users = null; Cvcuserdao dao = new Cvcuserdao (); arraylist<user> datas = Dao.getusers (); Gson Gson = new Gson (); users = Gson.tojson (datas); return users;}}
5. Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "><display-name>yab-cvc-ws</ 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>servletservice </servlet-name><servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer </ servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping>< Servlet-name>servletservice</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping></web-app>
A special reminder is that the servlet class, which uses different versions of the jar package, is a different t_t, and has been blown away.
Http://localhost:8080/YAB-CVC-WS/REST/webService/cvcuser
YAB-CVC-WS: Project Name
Rest:url-pattern
WebService: There are definitions in the WS class
Cvcuser: There are definitions in the WS class
An article on my big Pit.
Http://www.dineshonjava.com/2013/06/restful-web-services-with-jersey-jax-rs.html#.VxCy7Pl96Ul
Deploy 1.x Jersey webservices on Tomcat