CXF SpringMvc XML传参

----------服务端---------
xml 配置

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    

<jaxws:endpoint id="userOrg"
implementor="com.hotent.mini.ext.service.impl.UserOrgServiceWsdlImpl"
address="/userOrg">
</jaxws:endpoint> 

// 实现类
package com.hotent.mini.ext.service.impl;
public class UserOrgServiceWsdlImpl implements IUserOrgWsdlService {
@Override
public String getUserList(String xml) {   
   return xml; 
}
}
// 接口
package com.hotent.mini.ext.service;
@WebService(serviceName="serviceUserOrg", endpointInterface = "com.hotent.mini.ext.service.IUserOrgWsdlService", 
targetNamespace = "http://impl.webservice.platform.hotent.com/")
public interface IUserOrgWsdlService {
String getUserList(String xml);
} 

---------客户端--------

public static void main(String args[]) { 
// 第一种方法 ----------------------------------------------
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		// 创建客户端连接
		Client client = factory.createClient("http://127.0.0.1:8080/xx/service/userOrg?wsdl");
		Object[] res = null;
		try {
			QName operationName = new QName("http://impl.webservice.userorg.com/","findUser");//如果有命名空间需要加上这个,第一个参数为命名空间名称,调用的方法名称
			res = client.invoke(operationName, "admin");//后面为WebService请求参数数组
			System.out.println(res[0]);
		}catch (Exception e) {
			e.printStackTrace();
		}

 // 第二种方法 ----------------------------------------------
// 被<![CDATA[]]>这个标记所包含的内容将表示为纯文本 
String xmlData = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
"<accounts>" + 
"<account>" + 
"<accId>帐号ID(必填)</accId>" + 
"<userPasswordMD5>密码</userPasswordMD5>" + 
"<userPasswordSHA1>密码</userPasswordSHA1>" + 
"其中userPasswordSHA1标签代表SHA1加密后的密码,userPasswordMD5标签代表MD5加密后的密码" + 
"<name>姓名</name>" + 
"<sn>姓</sn>" + 
"<description>描述 </description>" + 
"<email>邮箱 </email>" + 
"<gender>性别</gender>" + 
"<telephoneNumber>电话号码</telephoneNumber>" + 
"<mobile>移动电话</mobile>" + 
"<startTime>用户的开始生效时间(YYYY-MM-DD HH:mm:SS)</startTime>" + 
"<endTime>用户结束生效时间(YYYY-MM-DD HH:mm:SS) </endTime>" + 
"<idCardNumber>身份证号码</idCardNumber>" + 
"<employeeNumber>工号 </employeeNumber>" + 
"<o>用户所属的组织的编码号 </o>" + 
"<employeeType>用户类型</employeeType>" + 
"<supporterCorpName>所在公司名称 </supporterCorpName>" + 
"</account>" + 
"</accounts>]]>";

//调用方法
String method = "sayHello";
   method = "getUserList";
   
String data="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://impl.webservice.platform.hotent.com/\">"+
   "<soapenv:Body>"+
      "<ser:"+method+">"+
         "<arg0>"+ xmlData + "</arg0>"+
      "</ser:"+method+">"+
   "</soapenv:Body>"+
"</soapenv:Envelope>";
String httpUrl="http://127.0.0.1:8080/xx/service/helloWorld?wsdl";
   httpUrl="http://127.0.0.1:8080/xx/service/userOrg?wsdl";
try {
//第一步:创建服务地址  
   URL url = new URL(httpUrl);  
   //第二步:打开一个通向服务地址的连接  
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
   //第三步:设置参数  
   //3.1发送方式设置:POST必须大写  
   connection.setRequestMethod("POST");  
   //3.2设置数据格式:content-type  
   connection.setRequestProperty("content-type", "text/xml;charset=utf-8");  
   //3.3设置输入输出,因为默认新创建的connection没有读写权限,  
   connection.setDoInput(true);  
   connection.setDoOutput(true);  

   //第四步:组织SOAP数据,发送请求  
   String soapXML = data;
   //将信息以流的方式发送出去
   // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
   
   OutputStream os = connection.getOutputStream();  
   os.write(soapXML.getBytes());  
   //第五步:接收服务端响应,打印  
   int responseCode = connection.getResponseCode();  
   System.out.println("responseCode: "+responseCode);
   if(200 == responseCode){//表示服务端响应成功  
   //获取当前连接请求返回的数据流
       InputStream is = connection.getInputStream();  
       InputStreamReader isr = new InputStreamReader(is);  
       BufferedReader br = new BufferedReader(isr);  
         
       StringBuilder sb = new StringBuilder();  
       String temp = null;  
       while(null != (temp = br.readLine())){  
           sb.append(temp);  
       }  
       
       is.close();  
       isr.close();  
       br.close(); 
       System.out.println(StringEscapeUtils.unescapeXml(sb.toString()));    //转义
       System.out.println(sb.toString());  
       
    } else { //异常信息
   InputStream is = connection.getErrorStream();    //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。  
   InputStreamReader isr = new InputStreamReader(is,"utf-8");  
   BufferedReader in = new BufferedReader(isr);  
   String inputLine;  
   BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(  
           new FileOutputStream("d:\\result.xml")));// 将结果存放的位置  
   while ((inputLine = in.readLine()) != null)   
   {  
       System.out.println(inputLine);  
       bw.write(inputLine);  
       bw.newLine();  
       bw.close();  
   }  
   in.close(); 
   }
   
   os.close(); 
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

// 把xml转义
public static String escapeXml(String xml) {
String newXml = xml.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;").replaceAll("\"", "&quot;");
return newXml;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CXF是一个开源的Web服务框架,可以用于开发和部署SOAP和REST风格的Web服务。Spring MVC是一个基于Java的Web应用框架,用于开发和管理MVC(Model-View-Controller)模式的Web应用程序。 要集成CXF和Spring MVC,首先需要在项目中引入相关的jar包。可以通过在项目的pom.xml文件中添加依赖来实现,例如: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.4</version> </dependency> ``` 然后,在Spring MVC的配置文件(通常是一个XML文件,例如application-context.xml)中,需要配置CXF的相关内容。可以添加以下配置: ``` <jaxws:server id="helloService" address="/helloservice"> <jaxws:serviceBean> <bean class="com.example.HelloServiceImpl" /> </jaxws:serviceBean> </jaxws:server> ``` 在上面的配置中,`helloService`是服务的ID,`/helloservice`是服务的访问地址,`com.example.HelloServiceImpl`是实现了Web服务接口的类。 最后,在Spring MVC的控制器中,可以使用`@WebServiceRef`注解来引用CXF的Web服务。例如: ``` @Controller @RequestMapping("/hello") public class HelloController { @WebServiceRef private HelloService helloService; @RequestMapping(method = RequestMethod.GET) public String sayHello(Model model) { String message = helloService.sayHello(); model.addAttribute("message", message); return "hello"; } } ``` 在上面的示例中,`HelloService`是通过`@WebServiceRef`注解引用的CXF的Web服务接口,可以在控制器中直接调用相关的方法。 通过以上步骤,就可以实现CXF和Spring MVC的集成,从而开发和部署SOAP和REST风格的Web服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值