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;
	}

}

访问地址:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
去几年,REST逐渐成为影响Web框架、Web协议与Web应用设计的重要概念。如果你还不了解REST,那这个简短的介绍将有助你快速掌握REST,此外还可以点击这里了解关于REST的更多信息。 相关厂商内容 高速下载:Adobe Flash Builder 4 简体中文正式版 for Windows 高速下载:Adobe Flash Builder 4 简体中文正式版 for Mac 利用Flex SDK创建易于访问的Adobe AIR应用程序 Adobe和英特尔联手推新服务帮助开发者发行AIR应用 构建更加完善的Adobe AIR应用程序之十大秘诀 相关赞助商 汇集最新RIA技术相关资源,提供Flash开发平台相关工具高速下载,免费获得Adobe软件的产品序列号。 现在有越来越多的公司希望能以简单而又贴合Web架构本身的方式公开Web API,因此REST变得越来越重要也就不足为奇了。使用Ajax进行通信的富浏览器端也在朝这个目标不断迈进。这个架构原则提升了万维网的可伸缩性,无论何种应用都能从该原则中受益无穷。 JAX-RS(JSR 311)指的是Java API for RESTful Web Services,Roy Fielding也参与了JAX-RS的制订,他在自己的博士论文中定义了REST。对于那些想要构建RESTful Web Services的开发者来说,JAX-RS给出了不同于JAX-WS(JSR-224)的另一种解决方案。目前共有4种JAX-RS实现,所有这些实现都支持Spring,Jersey则是JAX-RS的参考实现,也是本文所用的实现。 如果你使用Spring进行开发,那可能想知道(或者有人曾问过你)Spring MVC与JAX-RS有何异同点?更进一步,如果你手头有一个Spring MVC应用,使用了控制类继承(SimpleFormController等),你可能还意识不到现在的Spring MVC对REST广泛的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值