一、server端配置:
1.在spring的配置文件中加入:<import resource="RESTful.xml" />
2.RESTful.xml文件内容如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:aop="http://www.springframework.org/schema/springSecurityFilterChainaop"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.4.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="bookservice" address="/">
<jaxrs:serviceBeans>
<ref bean="customerservice" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="demo.jaxrs.service.SecurityExceptionMapper" />
</jaxrs:providers>
</jaxrs:server>
<bean id="customerservice" class="demo.jaxrs.service.CustomerServiceImpl" />
<!-- <bean id="customerservice" class="demo.jaxrs.service.CustomerServiceSecuredImpl"/> -->
<security:global-method-security
secured-annotations="enabled">
<security:protect-pointcut
expression="execution(* demo.jaxrs.service.CustomerService.getCustomer(*))"
access="ROLE_CUSTOMER, ROLE_ADMIN" />
<security:protect-pointcut
expression="execution(* demo.jaxrs.service.CustomerService.addCustomer(*))"
access="ROLE_ADMIN" />
<security:protect-pointcut
expression="execution(* demo.jaxrs.service.CustomerService.updateCustomer(Long,demo.jaxrs.service.Customer))"
access="ROLE_ADMIN" />
<security:protect-pointcut
expression="execution(* demo.jaxrs.service.CustomerService.deleteCustomer(*))"
access="ROLE_ADMIN" />
<security:protect-pointcut
expression="execution(* demo.jaxrs.service.CustomerService.getOrder(*))"
access="ROLE_CUSTOMER, ROLE_ADMIN" />
</security:global-method-security>
<security:http auto-config='true'>
<security:http-basic />
</security:http>
<security:authentication-provider>
<security:user-service>
<security:user name="bob" password="bobspassword"
authorities="ROLE_CUSTOMER" />
<security:user name="fred" password="fredspassword"
authorities="ROLE_CUSTOMER, ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</beans>
3.CustomerService.java内容如下:
package demo.jaxrs.service;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/customerservice/")
public interface CustomerService {
@GET
@Path("/customers/{id}/")
Customer getCustomer(@PathParam("id") String id);
@PUT
@Path("/customers/{id}")
Response updateCustomer(@PathParam("id") Long id, Customer customer);
@POST
@Path("/customers/")
Response addCustomer(Customer customer);
@DELETE
@Path("/customers/{id}/")
Response deleteCustomer(@PathParam("id") String id);
@Path("/orders/{orderId}/")
Order getOrder(@PathParam("orderId") String orderId);
}
4.CustomerServiceImpl.java内容如下:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package demo.jaxrs.service;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
public class CustomerServiceImpl implements CustomerService {
long currentId = 123;
Map<Long, Customer> customers = new HashMap<Long, Customer>();
Map<Long, Order> orders = new HashMap<Long, Order>();
public CustomerServiceImpl() {
init();
}
public Customer getCustomer(String id) {
System.out.println("----invoking getCustomer, Customer id is: " + id);
long idNumber = Long.parseLong(id);
Customer c = customers.get(idNumber);
return c;
}
public Response updateCustomer(Long id, Customer customer) {
System.out.println("----invoking updateCustomer, Customer name is: " + customer.getName());
Customer c = customers.get(id);
if (c == null || c.getId() != customer.getId()) {
throw new WebApplicationException();
}
Response r;
if (c != null) {
customers.put(customer.getId(), customer);
r = Response.ok(customer).build();
} else {
r = Response.notModified().build();
}
return r;
}
public Response addCustomer(Customer customer) {
System.out.println("----invoking addCustomer, Customer name is: " + customer.getName());
customer.setId(++currentId);
customers.put(customer.getId(), customer);
return Response.ok(customer).build();
}
public Response deleteCustomer(String id) {
System.out.println("----invoking deleteCustomer, Customer id is: " + id);
long idNumber = Long.parseLong(id);
Customer c = customers.remove(idNumber);
Response r;
if (c != null) {
r = Response.ok().build();
} else {
r = Response.notModified().build();
}
return r;
}
public Order getOrder(String orderId) {
System.out.println("----invoking getOrder, Order id is: " + orderId);
long idNumber = Long.parseLong(orderId);
Order c = orders.get(idNumber);
return c;
}
final void init() {
Customer c = new Customer();
c.setName("John");
c.setId(123);
customers.put(c.getId(), c);
Order o = new Order();
o.setDescription("order 223");
o.setId(223);
orders.put(o.getId(), o);
}
}
5.server端例子可以参照cxf包中distribution\src\main\release\samples\jax_rs\spring_security的例子。
二、client端
1.在spring的配置文件中加入如下内容:<import resource="RESTClient.xml" />
2.RESTClient.xml内容如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="customerService" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory"
factory-method="create">
<constructor-arg type="java.lang.String"
value="http://192.168.1.101:8080/mvn_struts2/RESTful" />
<constructor-arg type="java.lang.Class"
value="demo.jaxrs.service.CustomerService" />
<constructor-arg type="java.lang.String" value="fred" />
<constructor-arg type="java.lang.String" value="fredspassword" />
<constructor-arg type="java.lang.String">
<null></null>
</constructor-arg>
</bean>
</beans>
3.则customerService即可当成spring的bean使用。
分享到:
相关推荐
Spring CXF Restful 实例
cxf集成Spring的restful WebServ ice接口,Demo的完整代码!
CXF+spring+restful创建一个web的接口项目源代码,添加了restful实现的配置及jar包
利用CXF3.0.2+Spring3.2.14发布RESTFul,传递json数据格式的!需要源代码可以下载!
利用CXF3.0.2+Spring3.2.14发布RESTFul,有源代码可以下载!
cxf集成Spring的restful WebService接口以Json形式表现的完整代码,适合新入门在学WebService的小伙伴们参考,希望对你们有帮助!
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
使用cxf、spring构建的rest风格webservice,其他相关技术springmvc、mybatis、druid等。代码中使用的数据库为sybase,请根据实际环境更改,需修改pom中引用的数据库驱动,依照entity类的属性建对应表,并修改config....
使用Spring整合CXF发布RESTFul项目案例(包含代码)
未引用spring, 单纯cxf restful 使用,只写了返回json数据的一个简单get请求例子
spring整合cxf实现restful的webservice服务,代码分为服务端和客户端两个模块;服务端为接口,客户端为接口调用。
这个版本是我上次传的CXF整合spring的加强版,增加了上传和下载已经拦截器的功能。如果还有不明白可以在留言里留下联系方式,大家可以一起讨论
附件是一个使用CXF 实现 RESTful Webservice 完整 CRUD 实例,使用 HashMap 来模拟数据库,可以直接运行,具体使用可以参考其中的 readme;仅供有兴趣的初学者参考。
采用spring 结合cxf 发布一个RestFul接口。 http请求,soap均可调用。
spring,cxf,restful发布webservice传递复杂对象,例如List,Map,List<Map>
spring + cxf + restful 方便初学者很快上手。 采用post和get请求两种方式。,
CXF Restful + Spring3 + Mybatis 后台开发环境搭建
spring + cxf + restful + soap 方便初学者很快上手。 注解描述 @Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数...
cxf_restful_webservice基于myeclipse 10 和tomcat 7开发
启动后访问http://localhost:8080/cxf-ws-restful-spring-server/ws63