刚刚配置完spring集成cxf使用restful方式部署webservice,整个过程感觉就是爽和简单,欢迎抛砖引玉
第一步:当然是下载jar包了
使用到的jar有以下:
1、spring jar包我就不说了,地球人都知道
2、cxf-2.6.9.jar 最新版是2.7X版本,我开始下的2.7版本,发现支持的JSR是2.0,起码需要JSR-339.api.jar,但是我下的是jsr311-api-1.1.1.jar,只能支持2.7以下版本,后来又重新下了cxf2.6.9在这里记录下避免忘记了。
3、jsr311-api-1.1.1.jar
4、geronimo-servlet_3.0_spec-1.0.jar
5、neethi-3.0.2.jar
第二步: 配置项目中web.xml
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
第三步:
新建appalicationcontext_cxf.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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">
<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="restSample" class="com.community.webservice.RESTSampleSource"/>
<!-- 这里的地址很重要,客户端需要通过这个地址来访问WebService -->
<jaxrs:server id="restServiceContainer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="restSample" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb"/>
</jaxrs:languageMappings>
</jaxrs:server>
</beans>
下面就是例子
新建Sample接口类
@Path(value = "/sample")
public interface Sample {
@GET
@Path("/bean/{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public String getBean(@PathParam("id") int id);
@GET
@Path("/list/{id}")
@Produces({ "application/xml","application/json" })
public String getBean2(@PathParam("id") int id);
}
新建Sample接口的实现类
@Path(value = "/sample")
public class RESTSampleSource implements Sample {
@Override
@GET
@Path("/bean/{id}")
@Produces({ "application/xml", "application/json" })
public String getBean(@PathParam("id") int id) {
return "<xml>1</xml>";
}
@Override
@GET
@Path("/list")
@Produces({ "application/xml","application/json" })
public String getBean2(@PathParam("id") int id) {
return "{id:'123'}";
}
}
好了,启动发布
使用
http://localhost:8080/cxfWebservice/rest/sample/bean/123 默认返回xml
http://localhost:8080/cxfWebservice/rest/sample/list?id=1&_type=json 加入_type=json则可以返回json数据
相关推荐
cxf集成Spring的restful WebServ ice接口,Demo的完整代码!
cxf集成Spring的restful WebService接口以Json形式表现的完整代码,适合新入门在学WebService的小伙伴们参考,希望对你们有帮助!
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
spring,cxf,restful发布webservice传递复杂对象,例如List,Map,List<Map>
spring整合cxf实现restful的webservice服务,代码分为服务端和客户端两个模块;服务端为接口,客户端为接口调用。
启动后访问http://localhost:8080/cxf-ws-restful-spring-server/ws63
使用cxf、spring构建的rest风格webservice,其他相关技术springmvc、mybatis、druid等。代码中使用的数据库为sybase,请根据实际环境更改,需修改pom中引用的数据库驱动,依照entity类的属性建对应表,并修改config....
spring + cxf + restful + soap 方便初学者很快上手。 注解描述 @Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数...
CXF+spring+restful创建一个web的接口项目源代码,添加了restful实现的配置及jar包
webservice-CXF-spring 实现server+client
cxf_restful_webservice基于myeclipse 10 和tomcat 7开发
主要为大家详细介绍了Spring整合CXF webservice restful的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring Boot 整合 Mybatis 实现RESTful API ,具体可以查看博客: http://blog.csdn.net/yaozhiqi1905658804/article/details/70820892
附件是一个使用CXF 实现 RESTful Webservice 完整 CRUD 实例,使用 HashMap 来模拟数据库,可以直接运行,具体使用可以参考其中的 readme;仅供有兴趣的初学者参考。
采用spring 结合cxf 发布一个RestFul接口。 http请求,soap均可调用。
使用了cxf 同时实现了 RESTful WebService --项目启动后访问地址 http://localhost:8080/springMVC/services/rest/equipQuery/getUserById/1?_type=json 本人没有一一列明 xml json 以及post get请求 大家可以在...
Springboot整合cxf测试项目亲测可用,精简版的boot承载cxf框架的资源!
自己在网上找了很多cxf实现rest风格...现在把源码发上来,java代码只有12K,主要是lib里面的全部jar包(spring、cxf等全部的jar包), 文章地址:http://blog.csdn.net/lxn39830435731415926/article/details/39448887
主要介绍了Spring Boot 实现Restful webservice服务端示例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
配置好数据库,放到tomcat访问 http://localhost:8080/Service wsdl:http://localhost:8080/Service/hello?wsdl wsal:http://localhost:8080/Service/rest?_wadl 访问rest实例:...