首先需要构建wsdl文件。
该demo使用的是springboot构建服务端。
目录结构:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cxb</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
<!-- <dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!--<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>-->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- mybatis的分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
#spring.datasource.url=jdbc:mysql://localhost:3306/test
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8089
#oracle
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=tenActivity
spring.datasource.password=root123456
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#jpa 配置这个才会自动生成表哦
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
User:
package com.cxb.hello.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
* Created by 81046 on 2018-08-21
*/
@Entity
@Table(name = "TB_WS_USER")
public class User implements Serializable {
private static final long serialVersionUID = -5939599230753662529L;
@Id
@Column(name = "USERID")
private Long userId;
@Column(name = "USERNAME")
private String username;
@Column(name = "EMAIL")
private String email;
@Column(name = "GMTCREATE")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date gmtCreate;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getGmtCreate() {
return gmtCreate;
}
public void setGmtCreate(Date gmtCreate) {
this.gmtCreate = gmtCreate;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", email='" + email + '\'' +
", gmtCreate=" + gmtCreate +
'}';
}
}
userDao:
package com.cxb.hello.dao;
import com.cxb.hello.model.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created by 81046 on 2018-08-23
*/
public interface UserDao {
@Select("select * from TB_WS_USER where userid = #{userId}")
User getUserById(Long userId);
@Select("select * from TB_WS_USER ")
List<User> getUserList();
}
WsUserService:
package com.cxb.hello.service;
import com.cxb.hello.model.User;
import java.util.List;
/**
* Created by 81046 on 2018-08-23
*/
public interface WsUserService {
String getName(Long userId);
User getUser(Long userId);
List<User> getUserList();
}
WsUserServiceImpl:
package com.cxb.hello.service.impl;
import com.cxb.hello.dao.UserDao;
import com.cxb.hello.model.User;
import com.cxb.hello.service.WsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by 81046 on 2018-08-23
*/
@Service
public class WsUserServiceImpl implements WsUserService {
@Autowired
private UserDao userDao;
@Override
public String getName(Long userId) {
User user = userDao.getUserById(userId);
if (user ==null){
return "未查到该用户的信息!"+userId;
}
return "查询的用户的信息-" + user.getUsername();
}
@Override
public User getUser(Long userId) {
return userDao.getUserById(userId);
}
@Override
public List<User> getUserList() {
return userDao.getUserList();
}
}
接下来就是webservice了:
注意这里的解决方法:需要在类前面注解@Service 标明该类也是由spring管理的。在application启动的时候根据名称获取bean对象。
webservice:
package com.cxb.hello.service;
import com.cxb.hello.model.User;
import com.cxb.hello.service.impl.WsUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;
/**
* Created by 81046 on 2018-08-21
*/
@WebService(targetNamespace="com.cxb.hello.service",endpointInterface = "com.cxb.hello.service.UserService")
public class UserService {
@Autowired
private WsUserServiceImpl wsUserService;
@WebMethod
public String getName(@WebParam(name = "userId") Long userId){
// System.out.println("接收客户端的userId:"+userId);
// String userInfo = new WsUserServiceImpl().getName(userId);
return "服务端返回值---userInfo***>"+userId;
/*User user = userDao.getUserById(userId);
if (user ==null){
return "未查到该用户的信息!"+userId;
}
return "查询的用户的信息-" + user.getUsername();*/
}
@WebMethod
public User getUser(@WebParam(name = "userId") Long userId){
return new WsUserServiceImpl().getUser(userId);
}
@WebMethod
public List<User> getUserList(){
return new WsUserServiceImpl().getUserList();
}
}
HelloApplication:
package com.cxb.hello;
import com.cxb.hello.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.xml.ws.Endpoint;
@SpringBootApplication
@WebListener
@MapperScan("com.cxb.hello.dao")
public class HelloApplication implements ServletContextListener {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//WebService的发布地址 这里的端口千万不要和该项目的端口一样,否则会报错
//http://127.0.0.1:8088/WebService?wsdl
String address = "http://127.0.0.1:8088/WebService";
//发布WebService,WebServiceImpl类是WebServie接口的具体实现类
Endpoint.publish(address , new UserService());
System.out.println("使用WebServicePublishListener发布webservice成功!");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
注意下面的红框起来的就是后面的解决问题方案:
最后访问:http://127.0.0.1:8088/WebService?wsdl
客户端实现:
目录结构:
test:
package com.cxb.test;
import java.util.concurrent.Callable;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* @author ChenXb
*
* 2018年8月23日
*/
public class Test {
public static void main(String[] args) {
try {
/*EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8088/WebService");
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(2*20000L);//超时时间20s
options.setTo(targetEPR);
*//**
* 参数:
* 1:在网页上执行 wsdl后xs:schema标签的targetNamespace路径
* <xs:schema targetNamespace="http://test.axiswevservice.com">
* 2:<xs:element name="test"> ======这个标签中name的值
*
*//*
QName qname = new QName("com.cxb.hello.service", "getName");
// String str = "客户端调用成功"; //方法的入参
Long userId=10001L;
Object[] param = new Object[]{userId.toString()};
Class<?>[] types = new Class[]{String.class}; //这是针对返值类型的
*//**
* RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
* invokeBlocking方法有三个参数
* 第一个参数的类型是QName对象,表示要调用的方法名;
* 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
* 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
*
* 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
*//*
Object[] response = sender.invokeBlocking(qname, param, types);
System.out.println(response[0]);*/
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress("http://127.0.0.1:8088/WebService");
// WSDL里面描述的接口名称(要调用的方法)
call.setOperationName(
new QName("com.cxb.hello.service", "getName"));
// call.setOperationName("commonMethod");
// 接口方法的参数名, 参数类型,参数模式 IN(输入), OUT(输出) or INOUT(输入输出)
call.addParameter("userId", XMLType.XSD_LONG, ParameterMode.IN);
// 设置被调用方法的返回值类型
call.setReturnType(XMLType.XSD_STRING);
Long a = 1001L;
// 设置方法中参数的值
Object[] paramValues = new Object[] {a.toString()};
// 给方法传递参数,并且调用方法
Object result = call.invoke(paramValues);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:客户端代码如图:
本来应该传的是long型的userId,但是不toString的时候,服务端接收不到值,只有toString才能接收到值。我也不知道为什么。
直接运行该main方法:客户端
服务端:
总结:该demo目前存在两处问题,如上,请各位有缘人帮忙看看,谢谢啦0.0
问题解决一个,可以操作数据库了,至于为什么要toString可能是api规定吧,我也不知道,反正能用就行啦,哈哈!!!