相当于schema文件的id
2.targetNamespace属性
用来指定schema文件的namespace的值
3.xmlns属性
引入一个约束,它的值是一个schema文件的namespace值
4.schemaLocation属性
用来指定引入的schema文件的位置
目录结构
新建一个接口类
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(“\n”);
APIUtils ap = new APIUtils();
User user = new User();
java.util.List list = new ArrayList();
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(“”);
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{
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
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(“用户和密码不正确”));
}
}
}
package jp.co.service;
public interface TestService {
public String SayHello();
}
package jp.co.service.impl;
import jp.co.service.TestService;
public class TestServiceImpl implements TestService {
@Override
public String SayHello() {
System.out.println(“功能方法被调用!”);
return “Hello 这是一个简单的WebService实例”;
}
}
调用方法
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">
<jaxws:endpoint id=“vo” implementor=“com.iflyee.cxf.Vo” address=“/vo”>
jaxws:inInterceptors
</jaxws:inInterceptors>
jaxws:outInterceptors
</jaxws:outInterceptors>
</jaxws:endpoint>
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">
<simple:server id=“testservice”
serviceClass=“jp.co.service.TestService” address=“/test”>
simple:serviceBean
</simple:serviceBean>
</simple:server>
web.xml
<?xml version="1.0" encoding="UTF-8"?>contextConfigLocation
WEB-INF/bean.xml
org.springframework.web.context.ContextLoaderListener
cxf
org.apache.cxf.transport.servlet.CXFServlet
1