标签:
<properties> <jersey.version>2.22.1</jersey.version> <spring.version>4.1.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-spring3</artifactId> <version>${jersey.version}</version> <!-- <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </exclusion> </exclusions> --> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-mvc</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey.version}</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.13</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version> </dependency> </dependencies>
web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>jersey</filter-name> <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.xwolf.jersey</param-value> </init-param> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value> </init-param> <init-param> <param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>jersey.config.servlet.filter.staticContentRegex</param-name> <param-value>/.*jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>jersey</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 激活annotation功能 --> <context:component-scan base-package="com.xwolf" /> <!-- 实例化占位符对象,加载数据库配置信息 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:db.properties</value> </list> </property> </bean> <!-- 配置数据源,并设置连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 设置数据库连接参数 --> <property name="url" value="${url}" /> <property name="driverClassName" value="${driverClassName}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 设置连接池参数 --> <property name="initialSize" value="${initSize}" /> <property name="maxActive" value="${maxSize}" /> <property name="filters" value="stat" /> </bean> <!-- 整合Mybatis框架 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:spring-mybatis.xml" /> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <list> <value>classpath:com/xwolf/jersey/mapper/*.xml</value> </list> </property> </bean> <!-- 配置扫描dao接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xwolf.jersey.dao"></property> </bean> <!-- 注入dataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 启用事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> <tx:method name="load*" propagation="SUPPORTS" read-only="true" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- AOP代理设置 --> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.xwolf.jersey.service..*.*(..))" advice-ref="txAdvice" /> </aop:config> </beans>
User.java:
package com.xwolf.jersey.bean; import java.util.Date; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private Long uid; private String name ; private String pwd; private Date birth; @XmlElement public Long getUid() { return uid; } public void setUid(Long uid) { this.uid = uid; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } @XmlElement public void setPwd(String pwd) { this.pwd = pwd; } @XmlElement public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "User [uid=" + uid + ", name=" + name + ", pwd=" + pwd + ", birth=" + birth + "]"; } }
userController.java;
package com.xwolf.jersey.controller; import java.io.IOException; import java.util.Date; import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.GET; 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; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.alibaba.fastjson.JSONObject; import com.xwolf.jersey.bean.User; import com.xwolf.jersey.service.UserService; @Controller @Singleton @Path("/user") public class UserController { @Autowired private UserService userService; /** * 返回字符串 * @return */ @GET @Path("/str") @Produces(MediaType.TEXT_PLAIN) public String str(){ return "Hello,Jersey !"; } /** * 返回xml * @return */ @GET @Path("/getXml") @Produces(MediaType.APPLICATION_XML) public User getUserXml(){ User user =new User(); user.setUid(234234l); user.setName("隔壁老王"); user.setPwd("rwerwe"); user.setBirth(new Date()); return user; } @GET @Path("/getJson") @Produces(MediaType.APPLICATION_JSON) public User getUserJson(){ User user =new User(); user.setUid(234234l); user.setName("隔壁老王"); user.setPwd("rwerwe"); user.setBirth(new Date()); return user; } /** * 用户注册 * @param uname * @param pwd * @return */ @POST @Path("/register") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public String register(@FormParam(value = "uname") String uname, @FormParam(value="pwd")String pwd){ User user = new User(); user.setName(uname); user.setPwd(pwd); user.setBirth(new Date()); JSONObject object = new JSONObject(); try { userService.add(user); object.put("success", true); object.put("msg", "注册成功!"); } catch (Exception e) { object.put("success", false); object.put("msg", "注册失败了!"); e.printStackTrace(); } return object.toJSONString(); } }
form.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注册</title> </head> <body> <form action="${pageContext.request.contextPath}/user/register" method="POST" > <table> <tr> <td>用户名:</td> <td><input name="uname"></td> </tr> <tr> <td>密码:</td> <td><input name="pwd"></td> </tr> <tr> <td><input type="submit" value="注册"></td> </tr> </table> </form> </body> </html>
【jersey】 spring 整合jersey 实现RESTful webservice
标签:
原文地址:http://www.cnblogs.com/lonelywolfmoutain/p/5097050.html