标签:
泪奔,调了两天终于调成了。先说说遇到的问题
Jersey 1.x 和 2.x项目内的config不同,且jersey 2.x 把 com.sun.jersey的路径改成了org.glassfish.jersey,这是最大的坑。笔者一开始没注意,开着1.x的教程用了2.x的包,即使后来发现了这个问题,但由于1.x陷的太深而改不回来了。
同学们会说,那改用1.x的包不就好啦。事实并非那么简单,jersey1.1和1.9的配置也不同,且官方现在只提供1.9的下载,下载下来有N多jar包,好吧,按照教程做,结果tomcat蹦了。仔细查看教程的tomcat版本是6.0,我用的是8.0.
这真是落泪了,变量一路增加,让我真是日了狗的心情都有了,两天调下来真是泪奔,还好最后发现了一篇靠谱的文章,现在来具体说说方法
环境
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
其实真正deploy ws时用到的只有3
项目结构
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(); data.setId(result.getInt("id")); data.setLoginID(result.getString("LoginID")); data.setName(result.getString("name")); data.setEmail(result.getString("email")); datas.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.email = email; } /** * @return the active */ public boolean isActive() { return active; } /** * @param active * the active to set */ public void setActive(boolean active) { this.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>
特别需要提醒的是servlet class,使用不同版本的jar包所用的class是不同的T_T,深受其害的飘过
http://localhost:8080/YAB-CVC-WS/REST/webService/cvcuser
YAB-CVC-WS:项目名
REST:URL-PATTERN
webService: 在WS类中有定义
cvcuser: 在WS类中有定义
附解我大坑的文章
http://www.dineshonjava.com/2013/06/restful-web-services-with-jersey-jax-rs.html#.VxCy7Pl96Ul
Deploy 1.x Jersey WebServices on Tomcat
标签:
原文地址:http://www.cnblogs.com/Raymond-Yang/p/5396340.html