cxf soap rest webservice spring
1. 导入 jar 包
2. 编写接口
3. 编写实现
4. 配置spring 配置文件
5. 配置web.xml servlet
6. 访问
package com.diancai.test; import javax.jws.WebParam;
import javax.jws.WebService; import com.diancai.service.dcrestaurant.WsConstants; @WebService(name="TestHelloword", targetNamespace=WsConstants.NS)
public interface TestHelloword {
String sayHi(@WebParam(name="name")String name);
}
package com.diancai.test; 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("/testHellowordRest")
public interface TestHellowordRest {
@GET
@Path("/say/{name}")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }) // 这里面的值是返回值能接受的类型
public String sayHello(@PathParam("name")String name); @GET
@Path("/sayHi/{name}")
@Produces(MediaType.APPLICATION_XML)
public HelloName sayHi(@PathParam("name")String name);
}
package com.diancai.test.impl; import javax.jws.WebService; import com.diancai.test.TestHelloword;
import com.diancai.service.dcrestaurant.WsConstants; @WebService(serviceName="TestHelloword",endpointInterface="com.diancai.test.TestHelloword",targetNamespace=WsConstants.NS)
public class TestHellowordImpl implements TestHelloword{ /**
* 注入service
*/ @Override
public String sayHi(String name) {
return "hello," +name;
} }
package com.diancai.test.impl; 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.diancai.test.HelloName;
import com.diancai.test.TestHellowordRest; @Path("/testHellowordRest")
public class TestHellowordRestImpl implements TestHellowordRest{ @GET
@Path("/say/{name}")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public String sayHello(@PathParam("name")String name) {
return "hello,"+name;
} @GET
@Path("/sayHi/{name}")
@Produces(MediaType.APPLICATION_XML)
public HelloName sayHi(@PathParam("name")String name){
HelloName a =new HelloName();
a.setName(name);
return a;
} }
使用的bean
package com.diancai.test; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; import com.diancai.service.dcrestaurant.WsConstants; @XmlRootElement
@XmlType(name="helloname",namespace=WsConstants.NS)
public class HelloName {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
package com.diancai.service.dcrestaurant;
/*
* webservice 命名空间
*/
public class WsConstants {
public static final String NS="http://diancai_service.com.diancai.sns";
}
<?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-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="true">
<description>Spring cxf 公共配置</description>
<!-- 访问 http://localhost:8082/cxf 查看发布的接口 --> <!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<!-- <context:component-scan base-package="com.diancai">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> --> <!-- <import resource="classpath:META-INF/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf-servlet.xml"/> --> <!-- 访问http://localhost:8082/cxf/soap/testHello?wsdl -->
<jaxws:endpoint id="testHelloword" address="/soap/testHello">
<jaxws:implementor ref="testHellowordImpl"/>
</jaxws:endpoint> <bean id="testHellowordImpl" class="com.diancai.test.impl.TestHellowordImpl"></bean> <!-- rest -->
<!-- 访问http://localhost:8082/cxf/jaxrs/testHellowordRest/say/1 -->
<jaxrs:server id="serviceContainer" address="/jaxrs">
<jaxrs:serviceBeans>
<ref bean="testHellowordRest"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
</jaxrs:providers>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml"></entry>
</jaxrs:extensionMappings>
</jaxrs:server>
<bean id="testHellowordRest" class="com.diancai.test.impl.TestHellowordRestImpl"></bean> </beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>web service</display-name> <!-- for soap ws -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- for cxf servlet -->
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping> <!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) -->
<!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) -->
<!-- 注意xml 文件 节点的顺序问题,有时候可能报错 -->
<!-- 不使用listener监听器来加载Spring 的配置,改用DispatcherServlet来加载spring的配置,简单 -->
<!-- for spring mvc -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 防止Spring mvc 乱码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
测试
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
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">
<!-- rs -->
<bean id="testHelloword" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8082/cxf/jaxrs/"></constructor-arg>
<constructor-arg type="java.lang.Class" value="com.diancai.webserivce.rest.TestHellowordRest"></constructor-arg>
</bean>
<!-- rs 还有一种方式 (注意org.apache.cxf.jaxrs.client.WebClient, 不是 jaxrsclientFactory ) -->
<!-- <bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8000/CXFWebService/rest/" />
</bean> -->
<!-- soap -->
<bean id="restauRantClent" class="com.diancai.webserivce.rest.TestHelloword" factory-bean="clientFactory" factory-method="create"></bean>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.diancai.webserivce.rest.TestHelloword"></property>
<property name="address" value="http://localhost:8082/cxf/soap/testHello"></property>
</bean>
</beans>
package com.diancai.webserivce.rest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Aa {
public static void main(String[] args){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
TestHelloword t=app.getBean("restauRantClent",TestHelloword.class);
System.out.println(t.getClass().getName()+"fds");
System.out.println(t.sayHi("张三"));
}
}
cxf soap rest webservice spring的更多相关文章
- 使用Spring和Tomcat发布CXF SOAP WebService
上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...
- WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)
1. 新建一个Maven项目,选择webapp模板,命名为WS_Spring_CXF_Tomcat 2. 在POM.xml中添加Spring和CXF的依赖 <!-- 添加 Spring depe ...
- 转载 WebService 的CXF框架 WS方式Spring开发
WebService 的CXF框架 WS方式Spring开发 1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...
- WebService 的CXF框架 WS方式Spring开发
1.建项目,导包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- 开发基于CXF的 RESTful WebService web 项目 webservice发布
配置步骤 开发基于CXF的 RESTful WebService 1.创建Web项目并导入CXF的jar 2.在Web.xml中配置 CXFServlet <servlet> <se ...
- CXF框架介绍及Spring集成
1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...
- Java 使用httpclient Post与cxf 发布的Webservice通信
使用cxf发布的webservice不知道什么情况总会有时管用有时不管用,对于项目来说这肯定不行.又不想改动webservice因为代码太多.人懒! 于是便使用httpclient与webservic ...
- CXF发布restful WebService的入门例子(客户端)
上篇说了怎么用cxf发布restful webservice,由于浏览器只能对该service发送http的GET请求,所以如果想对服务器上的数据,还需要实现客户端. 客户端的实现方式有无数种...可 ...
- THINKPHP3.2 中使用 soap 连接webservice 解决方案
今天使用THINKPHP3.2 框架中开发时使用soap连接webservice 一些浅见现在分享一下, 1.首先我们要在php.ini 中开启一下 php_openssl.dll php_soap. ...
随机推荐
- springboot-dokcer
项目就一个java文件,仅用于样例 package com.example.demo; import org.springframework.beans.factory.annotation.Valu ...
- jquery函数写法
普通jquery函数写法 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script&g ...
- spring data jpa 注解
@Data 注解引出的 lombok 小辣椒 今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok, ...
- leetcode13
public class Solution { private int ChangeToInt(char c) { ; string s = c.ToString(); switch (s) { ca ...
- Firefox内存占用过高解决方法
刚开始使用firefox火狐浏览器的时候,你会发现firefox占用内存大,CPU占用率高,打开网页停顿等问题,其实这些是因为firefox没有进行优化,默认设置是标准的设置的原因,解决方法如下: 一 ...
- Linq相关
Linq(语言集成查询) 相关资料如下: 1. Linq语言集成查询 百度百科 2. 30分钟Linq教程 3. Linq查询表达式(C#编程指南) 4. Linq十个常用查询 5. Linq技术专题 ...
- mongodb基础学习11-复制集和分片结合使用
实际的使用中复制集和分片是结合使用的,即一个分片由一个复制集构成,多个分片存储数据库的数据 调用脚本启动两个复制集 启动configsvr的节点 启动mongos 增加分片,这次要加上复制集的id,节 ...
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 视图Nunjucks
视频地址:https://www.cctalk.com/v/15114923888328 视图 Nunjucks 彩虹是上帝和人类立的约,上帝不会再用洪水灭人. 客户端和服务端之间相互通信,传递的数据 ...
- Python中文件编码的检测
前言: 文件打开的原则是“ 以什么编码格式保存的,就以什么编码格式打开 ”,我们常见的文件一般是以“ utf-8 ”或“ GBK ”编码进行保存的,由于编辑器一般设置了默认的保存和打开方式,所以我们在 ...
- 使用Font Awesome替换你的网站图标(icons 图标)
http://www.thinkcmf.com/font/icons/ 第一次使用 Font Awesome 发现相当的爽呀!它的图标很全,能够帮你节约时间去找图片.下面就来一起学习吧: 1: 去官方 ...