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