CXF Rest


1 interface

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.bean.User;

@Path(value="/user")
public interface UserRest {
	
	@GET
	@Path("/getUserById/{id}")
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public User getUserById(@PathParam("id") long id);
	
	@GET
    @Produces(MediaType.TEXT_PLAIN)
    public String doGet();
    
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/request/{param}")
    public String doRequest(@PathParam("param") String param, 
            @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse);

}


2  Impl

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.bean.User;
import com.svc.UserRest;

@Path(value = "/user")
public class UserRestImpl implements UserRest {

	@Override
	@GET
	@Path("/getUserById/{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public User getUserById(@PathParam("id")long id){
		User u=new User();
		u.setId(23432l);
		u.setName("Jack");
		u.setEmail("abc@hauwei.com");
		u.setAddress("China SZ");
		return u;
	}

	@Override
	public String doGet() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String doRequest(String param, HttpServletRequest servletRequest,
			HttpServletResponse servletResponse) {
		// TODO Auto-generated method stub
		return null;
	}

}


3 application.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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	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"
	default-lazy-init="false">   
	
	<import resource="classpath:META-INF/cxf/cxf.xml"/>  
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>   
    
    <bean id="userRest" class="com.svc.impl.UserRestImpl"></bean>

	<jaxrs:server id="userRestContainer" address="/rest">
		<jaxrs:serviceBeans>
			<ref bean="userRest"></ref>
		</jaxrs:serviceBeans>
		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="applicatiojn/xml" />
		</jaxrs:extensionMappings>
		<jaxrs:languageMappings>
			<entry key="en" value="en-gb" />
		</jaxrs:languageMappings>
	</jaxrs:server>  
 
</beans>


4 web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Rest</display-name>
  
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/classes/applicationContext.xml
		</param-value>
	</context-param>
	
  <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  <servlet>
		<display-name>CXF Servlet</display-name>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<init-param>
			<param-name>hide-service-list-page</param-name>
			<param-value>false</param-value>
		</init-param>
		<init-param>
			<param-name>disable-address-updates</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>



5 http://localhost:8081/test/services/rest/user/getUserById/34

6

7






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值