您的位置:首页 > 编程语言 > Java开发

spring+cxf简单实现restful web serivce

2013-10-23 11:15 357 查看
一、依赖包

<!-- restful web service begin -->

<dependency>

<groupId>javax.ws.rs</groupId>

<artifactId>jsr311-api</artifactId> <!-- web service -->

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-frontend-jaxws</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http-jetty</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-frontend-jaxrs</artifactId>

<version>${cxf.version}</version>

</dependency> <!-- no declaration can be found for element 'jaxrs:server' -->

<!-- restful web service end -->

二、cxf配置文件

<?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: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.xsd

http://cxf.apache.org/jaxrs

http://cxf.apache.org/schemas/jaxrs.xsd

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd">

<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" />

<jaxrs:server id="serviceContainer" address="/">

<jaxrs:serviceBeans>

<ref bean="userService" />

</jaxrs:serviceBeans>

</jaxrs:server>

</beans>

三、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>web</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext-em.xml;classpath:applicationContext-cxf.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- cxf -->

<servlet>

<servlet-name>CXFServlet</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CXFServlet</servlet-name>

<url-pattern>/rest/*</url-pattern>

</servlet-mapping>

<filter>

<filter-name>struts</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

</web-app>

四、java接口

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;

@Path("/userService")

public interface UserService {

@GET

@Path("getUserById/{id}")

@Produces(MediaType.APPLICATION_XML)

UserDTO getUserById(@PathParam("id") int id);

@POST

@Path("addUser")

@Produces(MediaType.APPLICATION_XML)

String addUser(UserDTO dto);

}

五、类实现

import org.springframework.stereotype.Service;

@Service("userService")

public class UserServiceImpl implements UserService {

public UserDTO getUserById(int id) {

System.out.println("ok");

UserDTO user = new UserDTO();

user.setId(1);

user.setPassword("ppt");

user.setUsername("ppt");

return user;

}

@POST

@Path("addUser")

@Produces(MediaType.APPLICATION_XML)

String addUser(UserDTO dto);

}

六、dto

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "user")

public class UserDTO {

private int id;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

七、访问

1、直接在浏览器地址栏输入:http://localhost:8888/bbginterface/rest/userService/getUserById/12

2、httpclient方式访问:

public static void main(String[] args) throws Exception {

HttpClient client = new HttpClient();

String uri = "http://localhost:8888/bbginterface/rest/userService/getUserById/12";

GetMethod method = new GetMethod(uri);

client.executeMethod(method);

if(method.getStatusCode() == HttpStatus.SC_OK) {

String content = method.getResponseBodyAsString();

System.out.println(content);

}

}

3、另一种访问方式

1)通过代理生产对象实例,交给spring管理

public class MyProxy implements BeanFactoryPostProcessor{

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

String baseAddress = "http://127.0.0.1:8888/bbginterface/rest";

UserService userService = JAXRSClientFactory.create(baseAddress, UserService.class);

beanFactory.registerSingleton("userServiceTest", userService);

}

}

2)spring 配置

<bean class="com.csair.proxy.MyProxy"></bean>

<bean id="testJaxreCreation" class="com.csair.test.TestJaxreCreation">

<property name="userServiceTest" ref="userServiceTest"></property>

</bean>

3)测试

public class TestJaxreCreation {

private UserService userServiceTest;

public static void main(String[] args) {

String path = "applicationContext-test-cxf.xml";

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);

TestJaxreCreation test = context.getBean("testJaxreCreation", TestJaxreCreation.class);

UserDTO dto = test.userServiceTest.getUserById(12);

System.out.println(dto.toString());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: