webservice ------ jaxws集成到spring中

1.发布服务

集成jar包

spring的包
commons-logging-1.1.1.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
webservice的包
xbean-spring-2.7.jar
webservices-rt.jar
webservices-extra.jar
webservices-extra-api.jar
webservices-api.jar
jaxws-spring-1.8.jar

1.修改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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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">
	<display-name>JaxwsSpringDemo</display-name>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- Start WebService Config -->
	<servlet>
		<servlet-name>JAXWSServlet</servlet-name>
		<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>JAXWSServlet</servlet-name>
		<url-pattern>/service/welcome</url-pattern>
	</servlet-mapping>
	<!-- End WebService Config -->
</web-app>

2.编写applicationContext.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:ws="http://jax-ws.dev.java.net/spring/core"
	xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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.2.xsd
    http://jax-ws.dev.java.net/spring/core
    http://jax-ws.dev.java.net/spring/core.xsd
    http://jax-ws.dev.java.net/spring/servlet
    http://jax-ws.dev.java.net/spring/servlet.xsd">

	<context:component-scan base-package="hyacinth.ws" />

	<wss:binding url="/service/welcome">
		<wss:service>
			<ws:service bean="#welcomeService" />
		</wss:service>
	</wss:binding>

</beans>

3.指向需要发布的服务接口

package hyacinth.ws.service;

import hyacinth.ws.IWelcome;

import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.stereotype.Service;

/**
 * @ClassName:HelloService
 * @Description:TODO
 * @date 2014-12-19 下午2:45:22
 */

@Service("welcomeService")
@WebService(targetNamespace = "http://xxx.com")
@SOAPBinding(style = Style.RPC)
public class Welcome {

	@Resource(name = "welcome")
	private IWelcome welcome;

	@WebMethod(operationName = "say")
	public String sayWelcome(@WebParam(name="name") String name,@WebParam(name="age") int age) {
		System.out.println("年龄:" + age);
		return welcome.say(name);
	}
	/*
	 * @WebMethod public String sayHello(String name) { return welcome.say(name); }
	 */
}

使用axis2调用服务接口获取数据

package com.demo;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
/**
 * 通用的调用WebService的两种方法。(调用别人提供的WSDL报文)
 * @author zhw
 *
 */
public class AxisDemo {
	public static void main(String[] args) {
		callData();
	}
	
	public static String callData() {
//		cunURL = http://172.16.13.14:8001/sireports/services/DBActionManager?wsdl getCCTMRTRPTDVP
//		huURL = http://172.16.12.34:8001/sireports/services/DBActionManager?wsdl getRptSrvRegionOrder getRptUseropentot getRptSrvAgeRate
		String endpoint = "http://127.0.0.1:8081/JaxwsSpringDemo/service/welcome?wsdl";
		String namespace = "http://xxx.com";
		String method = "say";
		String data = getData("小黑", endpoint, method,namespace);
		return data;
	}

	/**
	 * 调用接口取数据
	 * @param date
	 * @param iurl
	 * @param method 
	 * @return 数据内容
	 */
	public static String getData(String name,String iurl,String method,String namespace){
		org.apache.axis.client.Service service = new org.apache.axis.client.Service();
		String result=null;
		String jsonResultString=null;
		try {
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(iurl);   
			call.setOperationName(new QName(namespace,method)); 
			/*
			 * call.addParameter( new QName(namespace,"name"),
			 * org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
			 */
			call.addParameter( "name",  
					org.apache.axis.encoding.XMLType.XSD_STRING,   
					javax.xml.rpc.ParameterMode.IN);
			call.addParameter( "age",  
					org.apache.axis.encoding.XMLType.XSD_STRING,   
					javax.xml.rpc.ParameterMode.IN);
			call.setEncodingStyle("UTF-8");
			
			// 设置返回值类型:
			call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String 
			call.setUseSOAPAction(true);   
			call.setSOAPActionURI("http://service.ws.hyacinth/"+method);
			result = (String) call.invoke(new Object[]{name,1});
			int length=result.length();
			jsonResultString=result.substring(1, length-1);//拿到结果中的json格式的字符串
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return jsonResultString;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值