码迷,mamicode.com
首页 > Web开发 > 详细

Apache CXF实现的RESTful形式的webservices

时间:2016-04-12 07:44:38      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:cxf restful webservice

我在上一篇文章(http://www.zifangsky.cn/2016/03/apache-cxf实现的soap形式的webservices/)中说了下如何实现SOAP方式的web service,这篇文章将继续说明如何实现RESTful形式的web service


(1)项目结构:

在这里,我没有新建一个项目,而是在上一个项目的基础上实现的,具体来说就是新建了3个文件,如下图所示:

技术分享

其中,User类是一个普通的实体类,RestService和RestServiceImpl类分别表示服务接口和它的实现类

(2)User.java:

package cn.zifangsky.entity;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="userInfo")
public class User {
	private int id;
	private String name;
	private String contact;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContact() {
		return contact;
	}
	public void setContact(String contact) {
		this.contact = contact;
	}

}

(3)RestService.java:

package cn.zifangsky.service;

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 cn.zifangsky.entity.User;

public interface RestService {

	@GET	
	@Path(value="/user/{id}")
	@Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public User getUser(@PathParam("id") int id);
}

(4)RestServiceImpl.java:

package cn.zifangsky.service.impl;

import cn.zifangsky.entity.User;
import cn.zifangsky.service.RestService;

public class RestServiceImpl implements RestService {
	public User getUser(int id) {
		User user = new User();
		user.setId(id);
		user.setName("zifangsky");
		user.setContact("http://www.zifangsky.cn");
		return user;
	}

}

(5)web.xml不变,修改service-beans.xml:

添加<jaxrs:server></jaxrs:server>节点,表示是RESTful服务

<jaxrs:server id="userRest" address="/services/rest">
		<jaxrs:serviceBeans>
			<bean class="cn.zifangsky.service.impl.RestServiceImpl" />
		</jaxrs:serviceBeans>
		
		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
		</jaxrs:extensionMappings>
		
	</jaxrs:server>

添加之后,service-beans.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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.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 ">
	
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature" />
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<jaxws:server id="sayHelloServices" serviceClass="cn.zifangsky.service.CXFService" address="/services/soap" >
		<jaxws:serviceBean>
			<bean class="cn.zifangsky.service.impl.CXFServiceImpl" />
		</jaxws:serviceBean>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:features>
			<ref bean="loggingFeature" />
			<wsa:addressing xmlns:wsa="http://cxf.apache.org/ws/addressing" />
		</jaxws:features>
		
	</jaxws:server>
	
	<jaxrs:server id="userRest" address="/services/rest">
		<jaxrs:serviceBeans>
			<bean class="cn.zifangsky.service.impl.RestServiceImpl" />
		</jaxrs:serviceBeans>
		
		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
		</jaxrs:extensionMappings>
		
	</jaxrs:server>
	
</beans>

(6)测试:

整个配置到这里已经就完成了,在tomcat中启动项目,效果是这样:

技术分享

在浏览器中访问:http://localhost:8080/CXFDemo/services/rest/user/9

技术分享

可以看出,结果是正确,到此全部配置结束


本文出自 “zifangsky的个人博客” 博客,请务必保留此出处http://983836259.blog.51cto.com/7311475/1762762

Apache CXF实现的RESTful形式的webservices

标签:cxf restful webservice

原文地址:http://983836259.blog.51cto.com/7311475/1762762

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!