先搭建一个普通的springboot项目,这里不再赘述。配置文件:
server:
port: 8080
servlet.contextPath: /webservice
spring:
application:
name: webservice
cloud:
nacos:
discovery:
ip: 172.16.130.181
server-addr: 172.16.205.54:8048
#server-addr: 127.0.0.1:8848
nacos中为普通数据库配置就不贴出来了
1、依赖包引用
<!--axis2 begin-->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-jaxws</artifactId>
<version>1.7.7</version>
</dependency>
<!--axis2 end-->
2、services.xml配置文件创建
首先创建基本路径:WEB-INF/services/axis2/META-INF/services.xml
注:路径名必须和上面一致
services.xml内容
<?xml version="1.0" encoding="UTF-8" ?>
<serviceGroup>
<service name="helloService" scope="application" targetNamespace="http://service.axis2.webservicedemo.gaozhen.com" >
<description>simple axis2 webservice example</description>
<schema schemaNamespace="http://service.axis2.webservicedemo.gaozhen.com" />
<parameter name="ServiceClass">com.gaozhen.webservicedemo.axis2.service.HelloService</parameter>
<operation name="sayHello">
<messageReceiver class = "org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
里面com.gaozhen.....为项目包路径
3、创建上路包路径和配置类
Axis2WsClient主要用于测试webservice调用结果(可以不用,用soapUI来测试也可以),内容如下:
package com.gaozhen.webservicedemo.axis2.client;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
/**
* Created by 11865 on 2020/6/2.
*/
public class Axis2WsClient {
public static void callWs() throws AxisFault {
try {
EndpointReference targetEPR = new EndpointReference("http://172.16.130.181:8080/webservice/services/helloService?wsdl");
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(2 * 20000L); //超时时间20s
options.setTo(targetEPR);
QName qName = new QName("http://service.axis2.webservicedemo.gaozhen.com", "sayHello");
String name = "i am gaozhen";
Object[] param = new Object[]{name};
// Object[] param = new Object[]{};
//这是针对返值类型的
Class<?>[] types = new Class[]{String.class};
Object[] response = sender.invokeBlocking(qName, param, types);
System.out.println(response[0]);
} catch (AxisFault e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
callWs();
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}
Axis2WebServiceConfiguration是webservice最主要的配置类,主要作用为读取services.xml配置文件,内容如下:
package com.gaozhen.webservicedemo.axis2.config;
import com.gaozhen.webservicedemo.axis2.util.FileCopyUtils;
import org.apache.axis2.transport.http.AxisServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/**
* Created by 11865 on 2020/6/2.
*/
@Configuration
public class Axis2WebServiceConfiguration {
@Bean
public ServletRegistrationBean axis2Servlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new AxisServlet());
servletRegistrationBean.addUrlMappings("/services/*");
// 通过默认路径无法找到services.xml,这里需要指定一下路径,且必须是绝对路径
String path = this.getClass().getResource("/WEB-INF").getPath().toString();
if (path.toLowerCase().startsWith("file:")) {
path = path.substring(5);
}
if (path.indexOf("!") != -1) {
try {
FileCopyUtils.copy("WEB-INF/services/axis2/META-INF/services.xml");
} catch (IOException e) {
e.printStackTrace();
}
path = path.substring(0, path.lastIndexOf("/", path.indexOf("!"))) + "/WEB-INF";
}
//System.out.println("xml配置文件,path={ "+path+" }");
servletRegistrationBean.addInitParameter("axis2.repository.path", path);
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
}
HelloService为业务测试类,你的主要业务体现,内容如下:
package com.gaozhen.webservicedemo.axis2.service;
public class HelloService {
public String sayHello(String name) {
return "hello, " + name;
}
}
FileCopyUtils上面Axis2WebServiceConfiguration配置类有用到,主要作用确保service.xml的调用,代码如下:
package com.gaozhen.webservicedemo.axis2.util;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 将jar内的文件复制到jar包外的同级目录下
*/
public class FileCopyUtils {
private static final Logger log = LoggerFactory.getLogger(FileCopyUtils.class);
private static InputStream getResource(String location) throws IOException {
InputStream in=null;
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
in = resolver.getResource(location).getInputStream();
byte[] byteArray = IOUtils.toByteArray(in);
return new ByteArrayInputStream(byteArray);
}catch (Exception e){
e.printStackTrace();
log.error("getResource is error: {}", e);
return null;
}finally {
if (in != null) {
in.close();
}
}
}
/**
* 获取项目所在文件夹的绝对路径
* @return
*/
private static String getCurrentDirPath() {
URL url = FileCopyUtils.class.getProtectionDomain().getCodeSource().getLocation();
String path = url.getPath();
if(path.startsWith("file:")) {
path = path.replace("file:", "");
}
if(path.contains(".jar!/")) {
path = path.substring(0, path.indexOf(".jar!/")+4);
}
File file = new File(path);
path = file.getParentFile().getAbsolutePath();
return path;
}
private static Path getDistFile(String path) throws IOException {
String currentRealPath = getCurrentDirPath();
Path dist = Paths.get(currentRealPath + File.separator + path);
Path parent = dist.getParent();
if(parent != null) {
Files.createDirectories(parent);
}
Files.deleteIfExists(dist);
return dist;
}
/**
* 复制classpath下的文件到jar包的同级目录下
* @param location 相对路径文件,例如kafka/kafka_client_jaas.conf
* @return
* @throws IOException
*/
public static String copy(String location) throws IOException {
InputStream in = getResource("classpath:"+location);
Path dist = getDistFile(location);
Files.copy(in, dist);
in.close();
return dist.toAbsolutePath().toString();
}
}
4、启动项目
出现以上日志说明成功。
5、测试
先用网址访问:http://172.16.130.181:8080/webservice/services/helloService?wsdl
如下图说明成功:
用Axis2WsClient测试,直接右键启动运行即可
标识成功