springboot集成webservice

springboot集成webservice做接口

1.首先搭建springboot项目,如下是pom文件所需引入架包
<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</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>org.jdom</groupId>
			<artifactId>jdom2</artifactId>
			<version>2.0.6</version>
		</dependency>


		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>

		<dependency>
			<groupId>xom</groupId>
			<artifactId>xom</artifactId>
			<version>1.3.2</version>
		</dependency>

        <!-- 调用腾讯健康卡接口引入包 -->
        <dependency>
            <groupId>cn.ucmed</groupId>
            <artifactId>open-platform-sdk</artifactId>
            <version>1.0.2</version>
        </dependency>


		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>


		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			 <version>4.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			 <version>4.4.10</version>
		</dependency>



		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
2.创建服务接口
package com.example.service;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


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

    @WebMethod
    String registerHealthCard(@WebParam(name = "registerHealthCard") String reqString);

    @WebMethod
    String verifyQRCode(@WebParam(name = "verifyQRCode") String reqString);
}
3.创建服务端接口实现类
package com.example.service;
import com.ucmed.aqslyy.util.TencentUtil;
import com.ucmed.aqslyy.util.XmlUtil;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.apache.log4j.Logger;
import org.jdom2.JDOMException;
import javax.jws.WebService;
import java.io.IOException;
@WebService(serviceName = "HealthCardService", // 与接口中指定的name一致
        targetNamespace = "http://service.example.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.example.service.HealthCardService"// 接口地址
)
public class HealthCardServiceImpl implements HealthCardService {
    private static final Logger LOG = Logger.getLogger(HealthCardServiceImpl.class);


    @Override
    public String registerHealthCard(String reqString) {
        //在这里进行处理业务逻辑
        return "";
    }

    @Override
    public String verifyQRCode(String reqString) {
       //在这里进行处理业务逻辑
        return "";
    }
}

4.创建CXF配置类
package com.example.service;


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 {

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

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

    @Bean
    public HealthCardService healthCardService() {
        return new HealthCardServiceImpl();
    }

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

}

5.这里对启动了进行了修改 ,@ComponentScan 的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
package com.ucmed.example;

import com.ucmed.aqslyy.service.HealthCardServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import javax.xml.ws.Endpoint;


@SpringBootApplication
@ComponentScan(basePackages = "com.example.*")
public class AqslyyApplication {

	public static void main(String[] args) {
		SpringApplication.run(AqslyyApplication.class, args);
		
	}

}

下面直接启动成功后,在浏览器直接访问就可以看到了http://127.0.0.1:8080/healthCard/api?wsdl

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值