web.xml配置Axis1.4

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <display-name>Apache-Axis Servlet</display-name>
    8. <servlet-name>AxisServlet</servlet-name>
    9. <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    10. </servlet>
    11. <servlet>
    12. <display-name>Axis Admin Servlet</display-name>
    13. <servlet-name>AdminServlet</servlet-name>
    14. <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
    15. <load-on-startup>100</load-on-startup>
    16. </servlet>
    17. <servlet>
    18. <display-name>SOAPMonitorService</display-name>
    19. <servlet-name>SOAPMonitorService</servlet-name>
    20. <servlet-class>org.apache.axis.monitor.SOAPMonitorService</servlet-class>
    21. <init-param>
    22. <param-name>SOAPMonitorPort</param-name>
    23. <param-value>5101</param-value>
    24. </init-param>
    25. <load-on-startup>100</load-on-startup>
    26. </servlet>
    27. <servlet-mapping>
    28. <servlet-name>AxisServlet</servlet-name>
    29. <url-pattern>/servlet/AxisServlet</url-pattern>
    30. </servlet-mapping>
    31. <servlet-mapping>
    32. <servlet-name>AxisServlet</servlet-name>
    33. <url-pattern>*.jws</url-pattern>
    34. </servlet-mapping>
    35. <servlet-mapping>
    36. <servlet-name>AxisServlet</servlet-name>
    37. <url-pattern>/services/*</url-pattern>
    38. </servlet-mapping>
    39. <servlet-mapping>
    40. <servlet-name>SOAPMonitorService</servlet-name>
    41. <url-pattern>/SOAPMonitor</url-pattern>
    42. </servlet-mapping>
    43. <servlet-mapping>
    44. <servlet-name>AdminServlet</servlet-name>
    45. <url-pattern>/servlet/AdminServlet</url-pattern>
    46. </servlet-mapping>
    47. <mime-mapping>
    48. <extension>wsdl</extension>
    49. <mime-type>text/xml</mime-type>
    50. </mime-mapping>
    51. </web-app>

    配置WEB-INF/server-config.wsdd文件注册Web Service服务类和方法:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    3. <globalConfiguration>
    4. <parameter name="sendMultiRefs" value="true"/>
    5. <parameter name="disablePrettyXML" value="true"/>
    6. <parameter name="adminPassword" value="admin"/>
    7. <parameter name="dotNetSoapEncFix" value="true"/>
    8. <parameter name="enableNamespacePrefixOptimization" value="false"/>
    9. <parameter name="sendXMLDeclaration" value="true"/>
    10. <parameter name="sendXsiTypes" value="true"/>
    11. <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
    12. <requestFlow>
    13. <handler type="java:org.apache.axis.handlers.JWSHandler">
    14. <parameter name="scope" value="session"/>
    15. </handler>
    16. <handler type="java:org.apache.axis.handlers.JWSHandler">
    17. <parameter name="scope" value="request"/>
    18. <parameter name="extension" value=".jwr"/>
    19. </handler>
    20. </requestFlow>
    21. </globalConfiguration>
    22. <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
    23. <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
    24. <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
    25. <service name="AdminService" provider="java:MSG">
    26. <parameter name="allowedMethods" value="AdminService"/>
    27. <parameter name="enableRemoteAdmin" value="true"/>
    28. <parameter name="className" value="org.apache.axis.utils.Admin"/>
    29. <namespace>http://xml.apache.org/axis/wsdd/</namespace>
    30. </service>
    31. <service name="Version" provider="java:RPC">
    32. <parameter name="allowedMethods" value="getVersion"/>
    33. <parameter name="className" value="org.apache.axis.Version"/>
    34. </service>
    35. <service name="SOAPMonitorService" provider="java:RPC">
    36. <parameter name="allowedMethods" value="publishMessage"/>
    37. <parameter name="scope" value="Application"/>
    38. <parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/>
    39. </service>
    40. <service name="TestService" provider="java:RPC">
    41. <parameter name="className" value="com.anbai.sec.axis.TestService"/>
    42. <parameter name="allowedMethods" value="*"/>
    43. </service>
    44. <service name="FileService" provider="java:RPC">
    45. <parameter name="className" value="com.anbai.sec.axis.FileService"/>
    46. <parameter name="allowedMethods" value="readFile,writeFile"/>
    47. </service>
    48. <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler">
    49. <parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/>
    50. <parameter name="serviceName" value="SOAPMonitorService"/>
    51. <parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
    52. <parameter name="portName" value="Demo"/>
    53. </handler>
    54. <transport name="http">
    55. <requestFlow>
    56. <handler type="URLMapper"/>
    57. <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
    58. <!--comment following line for REMOVING wsdl spying via SOAPMonitor-->
    59. <handler type="soapmonitor"/>
    60. </requestFlow>
    61. <responseFlow>
    62. <!--comment following line for REMOVING wsdl spying via SOAPMonitor-->
    63. <handler type="soapmonitor"/>
    64. </responseFlow>
    65. <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
    66. <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
    67. <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
    68. <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
    69. <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
    70. <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
    71. </transport>
    72. <transport name="local">
    73. <responseFlow>
    74. <handler type="LocalResponder"/>
    75. </responseFlow>
    76. </transport>
    77. </deployment>

    FileService类,提供了文件读写接口:

    1. package com.anbai.sec.axis;
    2. import org.apache.commons.io.FileUtils;
    3. import java.io.File;
    4. import java.io.IOException;
    5. /**
    6. * @author yz
    7. */
    8. public class FileService {
    9. public String readFile(String path) {
    10. if (path != null && !"".equals(path)) {
    11. File file = new File(path);
    12. if (file.exists()) {
    13. try {
    14. return FileUtils.readFileToString(file, "UTF-8");
    15. } catch (IOException e) {
    16. return "读取文件:" + file + "异常:" + e;
    17. }
    18. } else {
    19. return "文件:" + file + "不存在!";
    20. }
    21. } else {
    22. return "path不能为空!";
    23. }
    24. }
    25. public String writeFile(String path, String content) {
    26. if (path != null && !"".equals(path)) {
    27. File file = new File(path);
    28. try {
    29. FileUtils.writeStringToFile(file, content, "UTF-8");
    30. return file.getAbsolutePath();
    31. } catch (IOException e) {
    32. return "写文件:" + file + "异常:" + e;
    33. }
    34. }
    35. return "path不能为空!";
    36. }
    37. public String test() {
    38. return "文件WebService测试~";
    39. }
    40. }

    使用IDEA创建Web Service项目默认会创建管理Web Service的API:/servlet/AxisServlet/servicesSOAPMonitor/servlet/AdminServlet*.jws以及用监控Web Service的端口50015101
    2. Axis1.4 配置 - 图1
    访问Web ServiceFileService服务加上?wsdl参数可以看到FileService提供的服务方法和具体的参数信息。
    2. Axis1.4 配置 - 图2
    使用SOAP-UI调用Web Service接口示例:
    2. Axis1.4 配置 - 图3
    需要注意的是Web Service也是可以设置授权认证的,如实现了WS-SecurityWSS4J
    2. Axis1.4 配置 - 图4
    使用IDEA根据wsdl生成Web Service客户端代码:
    2. Axis1.4 配置 - 图5
    设置wsdl地址、包名:
    2. Axis1.4 配置 - 图6
    新建FileServiceTest类测试接口调用:

    1. package com.anbai.sec.axis.client;
    2. import java.net.URL;
    3. /**
    4. * 文件Web Service服务测试
    5. *
    6. * @author yz
    7. */
    8. public class FileServiceTest {
    9. public static void main(String[] args) {
    10. try {
    11. FileServiceService fileService = new FileServiceServiceLocator();
    12. URL webServiceUrl = new URL("http://localhost:8080/services/FileService");
    13. FileServiceSoapBindingStub soapService = new FileServiceSoapBindingStub(webServiceUrl, fileService);
    14. String content = soapService.readFile("/etc/passwd");
    15. System.out.println(content);
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }

    参考:

    1. axis2 利用小工具cat.aar
    2. Axis1.4框架 实现webservice服务器和客户端
    3. 使用IDEA根据wsdl生成WebServices客户端代码-Java