1、Webservice是什么?
1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据)
2. 一个跨语言、跨平台的规范(抽象)
3. 多个跨平台、跨语言的应用间通信整合的方案(实际)
2、WebService中的几个重要术语
WSDL:web service definition language
直译 : WebService定义语言
1. 对应一种类型的文件 .wsdl
2. 定义了web service的服务器端与客户端应用交互传递请求和响应数据的格式和方式
3. 一个web service对应一个 唯一的wsdl文档
SOAP:simpleobject access protocal
直译: 简单对象访问协议
1.是一种简单的、基于HTTP和XML的协议, 用于在WEB上交换结构化的数据
2. soap消息:请求消息和响应消息
3 http+xml片段
SEI:WebServiceEndPoint Interface(终端)
直译: web service的终端接口,
- 就是WebService服务器端用来处理请求的接口
CXF:Celtix +XFire
一个apache的用于开发webservice服务器端和客户端的框架\
3、WebService的两个重要组成部分
http协议
1. 请求的组成: 请求行(请求方式 path http1.1)
请求头
请求体:只有post请求有
2. 响应的组成 响应状态行:
响应头
响应体: 浏览器解析显示的数据
3. 请求的过程
**
**
**
**
**Schema约束
**
1.namespace
相当于schema文件的id
2.targetNamespace属性
用来指定schema文件的namespace的值
3.xmlns属性
引入一个约束,它的值是一个schema文件的namespace值
4.schemaLocation属性
用来指定引入的schema文件的位置
目录结构
\
新建一个接口类
</pre><pre name="code" class="java">package com.iflyee.cxf;
import javax.jws.WebService;
@WebService
public interface Ivo {
public boolean vo(String username,int count);
public int getvousernameCount();
public int getvocount();
public String build01();
public String xml();
}
package com.iflyee.cxf;
import java.util.ArrayList;
import javax.jws.WebService;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
@WebService
public class Vo implements Ivo {
//添加属性
private static int pt;
private static int ut;
public int getvocount() {
// TODO Auto-generated method stub
return pt;
}
public int getvousernameCount() {
// TODO Auto-generated method stub
return ut;
}
public boolean vo(String username, int count) {
ut++;
pt+=count;
return true;
}
public String build01(){
//DocumentHelper提供了创建Document对象的方法
Document document = DocumentHelper.createDocument();
try {
//添加节点信息
Element rootElement = document.addElement("modules");
//这里可以继续添加子节点,也可以指定内容
rootElement.setText("这个是module标签的文本信息");
Element element = rootElement.addElement("module");
for (int i = 0; i < 5; i++) {
Element nameElement = element.addElement("name");
Element valueElement = element.addElement("value");
Element descriptionElement = element.addElement("description");
nameElement.setText("名称"+i);
nameElement.addAttribute("language", "java"+i);//为节点添加属性值
valueElement.setText("值"+i);
valueElement.addAttribute("language", "c#"+i);
descriptionElement.setText("描述"+i);
descriptionElement.addAttribute("language", "sql server"+i);
}
System.out.println(document.asXML()); //将document文档对象直接转换成字符串输出
} catch (Exception e) {
e.printStackTrace();
}
return document.asXML();
}
public String xml(){
StringBuffer str = new StringBuffer();
str.append("<body>\n");
APIUtils ap = new APIUtils();
User user = new User();
java.util.List<Object> list = new ArrayList<Object>();
list.add(1);
list.add("张胜男");
list.add("xs111");
list.add(2);
list.add("张胜");
list.add("xs222");
list.add(3);
list.add("胜男");
list.add("xs333");
for (int i = 0; i < 3; i++) {
str.append(ap.getXMLModel(user,list));
}
System.out.println(str);
str.append("</body>");
APIUtils.num=0;
return str.toString();
}
}
\
package com.iflyee.cxf;
public class User {
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;
}
}
package com.lzw.springcxf.auth;
import java.util.List;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public AuthInterceptor() {
//拦截器在调用方法之前拦截SOAP消息
super(Phase.PRE_INVOKE);
System.out.println("11111111111111111111111111111111111");
}
/** * @Description: 拦截器操作 * @param msg 被拦截到的SOAP消息 * @throws Fault */
@Override
public void handleMessage(SoapMessage msg) throws Fault {
System.out.println("==============================");
System.out.println("=====自定义拦截器=======");
//获取SOAP消息的Header
List<Header> headers = msg.getHeaders();
//如果没有Header
if(headers == null || headers.size() < 1) {
throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截"));
}
//获取Header携带是用户和密码信息
Header firstHeader = headers.get(0);
Element ele = (Element) firstHeader.getObject();
NodeList userIdEle = ele.getElementsByTagName("userId");
NodeList userPassEle = ele.getElementsByTagName("userPass");
if (userIdEle.getLength() != 1) {
throw new Fault(new IllegalArgumentException("用户Id格式不对"));
}
if (userPassEle.getLength() != 1) {
throw new Fault(new IllegalArgumentException("用户密码格式不对"));
}
//获取元素的文本内容
String userId = userIdEle.item(0).getTextContent();
String userPass = userPassEle.item(0).getTextContent();
if (!userId.equals("lyy") || !userPass.equals("123456")) {
throw new Fault(new IllegalArgumentException("用户和密码不正确"));
}
}
}
</pre><pre code_snippet_id="1652672" snippet_file_name="blog_20160419_7_751113" name="code" class="java">package jp.co.service;
public interface TestService {
<span style="white-space:pre"> </span>public String SayHello();
}
</pre><pre code_snippet_id="1652672" snippet_file_name="blog_20160419_9_4894787" name="code" class="java">package jp.co.service.impl;
import jp.co.service.TestService;
public class TestServiceImpl implements TestService {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public String SayHello() {
<span style="white-space:pre"> </span>System.out.println("功能方法被调用!");
<span style="white-space:pre"> </span>return "Hello 这是一个简单的WebService实例";
<span style="white-space:pre"> </span>}
}
\
调用方法
package com.testClient;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import jp.co.service.TestService;
public class TestClient {
public static void main(String[] args) {
//创建一个客户端的代理工厂
ClientProxyFactoryBean clientProxy = new ClientProxyFactoryBean() ;
clientProxy.setServiceClass(TestService.class);
clientProxy.setAddress("http://localhost:8080/WcxF/services/test");
TestService pic = (TestService)clientProxy.create();
System.out.println(pic.SayHello());
}
}
\
\
\
bean.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"
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">
<!-- 引入CXF的支持的文件,来源CXF的jar文件 -->
<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"/>
<!-- 描述相关的晚上服务 -->
<jaxws:endpoint id="vo" implementor="com.iflyee.cxf.Vo" address="/vo">
<!-- 配置IN拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<bean class="com.lzw.springcxf.auth.AuthInterceptor"></bean>
</jaxws:inInterceptors>
<!-- 配置OUT拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
cxf-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
<!--配置CXF的ws服务 -->
<simple:server id="testservice"
serviceClass="jp.co.service.TestService" address="/test">
<simple:serviceBean>
<!-- 配置CXF的服务的实现 -->
<bean class="jp.co.service.impl.TestServiceImpl"></bean>
</simple:serviceBean>
</simple:server>
</beans>
web.xml\
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!--添加spring的支持-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--添加CXF的框架-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern> </servlet-mapping> <display-name>WcxF</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
源码一:CXF+Spring+自定义拦截器 WebService实例源码下载:download.csdn.net/detail/qq_1…
源码二:根据实体类装换xml源码下载 : download.csdn.net/detail/qq_1…
今天的文章java WebService CXF Spring 自定义拦截器 附实例源码分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/14946.html