Springboot集成webservice接口

接口类

  1. 添加@WebService注解,name作为唯一标识
  2. 添加@SOAPBinding注解,指定rpc方式调用
  3. 需开放的方法添加@WebMethod注解
  4. 方法需要传参的话,参数添加@WebParam注解
@WebService(name = "commandService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface CommandService {

    @WebMethod
    public String command(@WebParam(name = "userid") String userid);
}

接口实现类

  1. 添加@Service注解,接口注册
  2. 添加@WebService注解,与接口@WebService注解 name名保持一致
@Service
@WebService(serviceName = "commandService")
@Slf4j
public class CommandServiceImpl implements CommandService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String command(String enterpriseId, String randVal, String pwdHash,
                          String command, String commandSign, int commandType,
                          int encryptAlgorithm, int hashAlgorithm, String commandVersion) {
        //接口实现逻辑
        
    }
    
}

接口发布

  1. 配置类中添加@Configuration进行注册
  2. cxfServletRegistration(),声明webservice接口对外一级路径
  3. endpointCmdService(),声明指定接口对外二级路径(方法名可自定义,一级路径+二级路径即为接口完整访问路径)
  4. 多个接口发布声明多个接口和接口对应二级路径即可
@Configuration
public class WebServiceConfig {

    @Autowired
    private CommandService commandService;

    @Autowired
    private CommandAckService commandAckService;

    @Bean
    public ServletRegistrationBean<CXFServlet> cxfServletRegistration() {
        ServletRegistrationBean<CXFServlet> registration = new ServletRegistrationBean(new CXFServlet(), "/lxl-manager-web/ws/*");
        return registration;
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public Endpoint endpointCmdService() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), commandService);
        endpoint.publish("/command");
        return endpoint;
    }

    @Bean
    public Endpoint endpointCmdAckService() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), commandAckService);
        endpoint.publish("/commandAck");
        return endpoint;
    }

}

接口访问路径

http: ip:port/lxl-manager-web/ws/command?wsdl

http: ip:port/lxl-manager-web/ws/commandAck?wsdl

经验分享 程序员 微信小程序 职场和发展