spring+JAX-RS实现Restfull接口 demo
JAX-RS是JAVA EE6 引入的一个新技术。 JAX-RS即Java API for RESTful Web Services,是一个Java 编程语言的应用程序接口,支持按照表述性状态转移(REST)架构风格创建Web服务。JAX-RS使用了Java SE5引入的Java注解来简化Web服务的客户端和服务端的开发和部署。
JAX-RS提供了一些注解将一个资源类,一个POJO Java类,封装为Web资源。
@Path,标注资源类或者方法的相对路径
@GET,@PUT,@POST,@DELETE,标注方法是HTTP请求的类型。
@Produces,标注返回的MIME媒体类型
@Consumes,标注可接受请求的MIME媒体类型
@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,例如@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。
基于JAX-RS实现的框架有Jersey,RESTEasy等。这两个框架创建的应用可以很方便地部署到Servlet 容器中,比如Tomcat,JBoss等。值得一提的是RESTEasy是由JBoss公司开发的,所以将用RESTEasy框架实现的应用部署到JBoss服务器上,可以实现很多额外的功能。
话不多说上例子:
首先需要配置web.xml,其中context-param配置的为spring-mvc.xml在其中import了spring-rs.xml
<import resource="spring-rs.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>myApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring-mvc.xml
</param-value>
</context-param>
<!-- 配置dispatcherServlet -->
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>aaa</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>aaa</servlet-name>
<url-pattern>/s/*</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc.xml中只需<import resource="spring-rs.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 使用注解方式完成映射 -->
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="com.wq.com"/>
<mvc:annotation-driven/><!-- 开启注解 -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="aspectMethod" class="com.wq.com.index.aspectmethod.AspectMethod"></bean>
<aop:config>
<aop:pointcut id="aspectPointcut" expression="execution(* com.wq.com..service..toIndex())"/>
<aop:aspect ref="aspectMethod">
<aop:before method="beforeMethod" pointcut-ref="aspectPointcut"/>
<aop:after method="afterMethod" pointcut-ref="aspectPointcut"/>
</aop:aspect>
</aop:config>
<bean id="advisorMethod" class="com.wq.com.index.aspectmethod.AdvisorMethod"></bean>
<aop:config>
<aop:pointcut id="advisorPointcut" expression="execution(* com.wq.com..service..toIndex())"/>
<aop:advisor advice-ref="advisorMethod" pointcut-ref="advisorPointcut"/>
</aop:config>
<import resource="spring-rs.xml"/>
</beans>
下列代码为spring-rs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<http-conf:conduit name="*.http-conduit">
<http-conf:client ReceiveTimeout="60000" ConnectionTimeout="20000"/>
</http-conf:conduit>
<bean id="peopleImpl" class="com.jaxrs.rest.PeopleImp"></bean>
<jaxrs:server address="/rs">
<jaxrs:serviceBeans>
<ref bean="peopleImpl"/>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
com.jaxrs.rest.PeopleImp此类中用到的people类就自己写吧!很简单
package com.jaxrs.rest;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
//@WebService
@Path("/people")
public class PeopleImp implements IPeople{
@GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST)
// @Produces(MediaType.APPLICATION_JSON)//指定服务数据类型
@Path("/query/{id}")//@Path("/query/{id}")就是将“/query”映射到方法上,“{id}”映射到参数上,多个参数,以“/”隔开,放到“{}”中
public People queryPeople(@PathParam(value = "id") String id) {
People p = new People();
if(id.equals("1")){
p.setName("wangqiang");
p.setId(1);
}else{
p.setName("你的id不对");
p.setId(-1);
}
return p;
}
@GET
// @Produces(MediaType.APPLICATION_JSON)
@Path("/queryAll")
public List<People> queryAllPeople() {
List<People> list = new ArrayList<People>();
People p = new People();
p.setName("wangqiang");
p.setId(1);
People p2 = new People();
p2.setName("alsjkhdnoasjkhdlas");
p2.setId(2);
list.add(p);
list.add(p2);
return list;
}
}
访问地址: