分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
第一步,添加jar包
axis1实现webservice所需jar包
axis-ant.jar
axis.jar
jaxrpc.jar
spring-remoting.jar
(加粗的为核心包,简单的发布用这四个即可)
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
activation.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
xalan.jar
xmlsec-1.2.1.jar
第二步,修改web.xml
- <!--axis 需要引入的 Servlet -->
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
第三步,接口实现类及对外发布的调用方法
webservice.Java(这里getStr() 方法提供给外部调用)
- package com.business.apps;
- import javax.xml.rpc.ServiceException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
- import com.business.apps.manager.TestWebService;
- @SuppressWarnings("deprecation")
- public class WebService extends ServletEndpointSupport {
- @Autowired
- private ApplicationContext applicationContext;
- @Autowired
- private TestWebService testWebService;
- @Override
- protected void onInit() throws ServiceException {
- // 初始化Spirng 配置
- applicationContext = super.getApplicationContext();
- testWebService = (TestWebService) applicationContext.getBean("testWebService");
- }
- /**
- *
- * @Description: 接口
- * @param
- * @return
- * @throws Exception
- * @author
- * @date
- * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
- */
- public String getStr(String jsonString) throws Exception {
- return String.valueOf(testWebService.getStr(jsonString));
- }
- }
上面接口涉及的两个处理类如下:
TestWebserviceImpl.java
- /**
- *
- */
- package com.business.apps.manager.impl;
- /**
- * @author
- * @version 创建时间:2017年1月5日 下午4:21:25
- *
- */
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.util.HashMap;
- import java.util.Map;
- import javax.jws.WebMethod;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import net.sf.json.JSONObject;
- import org.apache.commons.lang.StringUtils;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.transaction.annotation.Transactional;
- import com.business.apps.manager.TestWebService;
- public class TestWebServiceImpl implements TestWebService{
- @SuppressWarnings("null")
- @Transactional
- public String getStr(String jsonString)throws Exception {///接收参数为String,然后再返回一个字符串String,方法里面是Json处理
- JSONObject jsonOb = JSONObject.fromObject(jsonString);
- String serviceTicket = jsonOb.optString("serviceTicket");
- Map<String, String> result = new HashMap<String, String>();
- result.put("URL","http://www.baidu.com");
- JSONObject json = JSONObject.fromObject(result);
- return json.toString();
- }
- }
- /*@Component //这里是注解的方式发布接口,配置文件需要配置注解机制,此处略
- @WebService(serviceName = "/TestService", targetNamespace = "http://localhost:8080")
- public class TestWebServiceImpl implements TestWebService{
- @WebMethod
- @WebResult(targetNamespace = "http://localhost:8080")
- @Transactional
- @Override
- public String getWgceaUrl(String jsonString) {
- JSONObject jsonOb = JSONObject.fromObject(jsonString);
- String serviceTicket = jsonOb.optString("serviceTicket");
- Map<String, String> result = new HashMap<String, String>();
- result.put("URL","http://localhost:8080/*************");
- JSONObject json = JSONObject.fromObject(result);
- return json.toString();
- }
- }*/
TestWebservice.java(TestWebserviceImpl对应的接口)
- /**
- *
- */
- package com.business.apps.manager;
- /**
- * @author
- * @version 创建时间:2017年1月5日 下午4:22:45
- *
- */
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import javax.xml.ws.BindingType;
- import javax.xml.ws.soap.SOAPBinding;
- public interface TestWebService {
- public String getWgceaUrl(String jsonString)throws Exception;
- }
第四步,创建配置文件:在WEB-INF下新建文件“server-config.wsdd”
- <?xml version="1.0" encoding="UTF-8"?>
- <deployment
- xmlns="http://xml.apache.org/axis/wsdd/"
- xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
- <globalConfiguration>
- <parameter name="adminPassword" value="admin" />
- <parameter name="sendXsiTypes" value="true" />
- <parameter name="sendMultiRefs" value="true" />
- <parameter name="sendXMLDeclaration" value="true" />
- <parameter name="axis.sendMinimizedElements" value="true" />
- <requestFlow>
- <handler type="java:org.apache.axis.handlers.JWSHandler">
- <parameter name="scope" value="session" />
- </handler>
- <handler type="java:org.apache.axis.handlers.JWSHandler">
- <parameter name="scope" value="request" />
- <parameter name="extension" value=".jwr" />
- </handler>
- </requestFlow>
- </globalConfiguration>
- <handler name="Authenticate"
- type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
- <handler name="LocalResponder"
- type="java:org.apache.axis.transport.local.LocalResponder" />
- <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"
- />
- <service name="AdminService" provider="java:MSG">
- <parameter name="allowedMethods" value="AdminService" />
- <parameter name="enableRemoteAdmin" value="false" />
- <parameter name="className" value="org.apache.axis.utils.Admin" />
- <namespace>http://xml.apache.org/axis/wsdd/</namespace>
- </service>
- <service name="Version" provider="java:RPC">
- <parameter name="allowedMethods" value="getVersion" />
- <parameter name="className" value="org.apache.axis.Version" />
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper" />
- <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
- </requestFlow>
- </transport>
- <transport name="local">
- <responseFlow>
- <handler type="LocalResponder" />
- </responseFlow>
- </transport>
- <!-- 自定义服务 -->
- <service name="WebService" provider="java:RPC">
- <parameter name="className" value="com.business.apps.WebService" />
- </service>
- </deployment>
第五步、sping配置文件(Spring-axis.xml)配置bean(项目位置/WEB-INF/config/context/Spring-axis.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: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.0.xsd">
- <bean id="testWebService" class="com.business.apps.manager.impl.TestWebServiceImpl" />
- </beans>
第六步、web.xml里配置sping容器
- <!-- spring容器配置 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/config/context/Spring-axis.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
第七步、调试无错误后之后启动eclipse,在浏览器里输入 http://localhost:8080/services/WebService?wsdl 有xml显示后编写调用接口类
testWebServiceGet.java
- /**
- *
- */
- package com.business.apps;
- /**
- * @author
- * @version 创建时间:2017年1月5日 下午5:35:26
- *
- */
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import javax.xml.namespace.QName;
- import net.sf.json.JSONObject;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class testWebServiceGet {
- private String nameSpaceUri = "http://localhost:8080/services/WebService";
- //private String nameSpaceUri = "http://www.ntrl.gov.cn/services/WebService";
- private String wsdlUrl = nameSpaceUri + "?wsdl";
- private Service service;
- private Call call;
- public final void init() throws Exception {
- // 创建调用对象
- service = new Service();
- call = (Call) service.createCall();
- // 调用 远程方法
- call.setOperationName(new QName(nameSpaceUri, "getStr"));
- // 设置URL
- call.setTargetEndpointAddress(new URL(nameSpaceUri));
- }
- public final void testGet() throws Exception {
- Map<String, String> result = new HashMap<String, String>();
- result.put("urlInput","http://www.google.com");
- JSONObject json = JSONObject.fromObject(result);
- String jsonString =json.toString();
- // 执行远程调用,同时获得返回值
- String r = (String) call.invoke(new Object[] {jsonString});
- JSONObject jsons = JSONObject.fromObject(r);
- String jsonStrings =jsons.toString();
- System.out.println("jsonStrings=" + jsonStrings);
- }
- public static void main(String[] args) {
- testWebServiceGet test = new testWebServiceGet();
- try {
- test.init();
- test.testGet();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }