使用springBoot搭建一个webservice接口案例

项目结构
在这里插入图片描述
CxfConfig类

import com.namchow.webservice.service.CommonService;
import com.namchow.webservice.service.impl.CommonServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Autowired
    CommonService commonService;

    @Bean
    public ServletRegistrationBean dispatchServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

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

    @Bean
    public CommonService commonService() {
        return new CommonServiceImpl();
    }

    /**
     * JAX-WS
     **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), commonService());
        endpoint.publish("/CommonService");
        return endpoint;
    }

}

CommonService类

import javax.jws.WebService;

/**
 * 接口
 */
@WebService(name = "CommonService", // 暴露服务名称
        targetNamespace = "http://webService.CommonService.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {

    String submit(String strSend) throws Exception;
}

CommonServiceImpl类

package com.namchow.webservice.service.impl;

import com.namchow.webservice.service.CommonService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.io.*;
import java.net.Socket;
import java.net.URLDecoder;

/**
 * 接口实现
 */
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
        targetNamespace = "http://CommonService.service.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.service.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService {
    @Override
    @WebMethod(operationName="helloWord")
    public String submit(@WebParam(name = "s") String s) throws Exception {
    return "Hello Word";
    }
}

访问url:http://localhost:8081/services/CommonService?wsdl
参考:https://www.cnblogs.com/myitnews/p/12370308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值