当前位置:   article > 正文

Springboot+webService 接口开发_springboot+webservice 接口开发 可乐cc

springboot+webservice 接口开发 可乐cc

一,webservice是什么?

网上的解释很多,其实就是跨语言和操作系统的的远程调用技术。比如亚马逊,可以将自己的服务以webservice的服务形式暴露出来,我们就可以通过web调用这些,无论我们使用的语言是java还是c,这也是SOA应用一种表现形式。

WSDL(Web Services Description Language)将无论用何种语言书写的web service描述出来,比如其参数或返回值。WSDL是服务端和客户端都能解读的标准格式。客户端通过URL地址访问到WSDL文件,在调用服务端之前先访问WSDL文件。 读取到WSDL后通过客户端的API类可以生成代理类,调用这些代理类就可以访问webservice服务。代理类将客户端的方法变为soap(Simple Object Access Protocol,可以理解为http+xml)格式通过http发送,同时接受soap格式的返回值并解析。

二,接口开发

  2.1,pom.xml 依赖

     重点:这里注意一下你的  springboot 版本,不同的版本 cxf 的写法不同  我这里是  2.0.8 版本

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.8.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

  2.2,cxf依赖:

  1. <!-- webservice cxf -->
  2. <dependency>
  3. <groupId>org.apache.cxf</groupId>
  4. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  5. <version>3.3.3</version>
  6. </dependency>

  2.3 , 服务端:

   endpoint.getInInterceptors().add(new WsInterceptor());

  此处只是加了特殊过滤器,无需要可以注释

  1. import com.techhero.platform.api.service.GfService;
  2. import com.techhero.platform.api.service.Impl.GfServiceImpl;
  3. import com.techhero.platform.api.util.WsInterceptor;
  4. import org.apache.cxf.Bus;
  5. import org.apache.cxf.bus.spring.SpringBus;
  6. import org.apache.cxf.jaxws.EndpointImpl;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import javax.xml.ws.Endpoint;
  10. /**
  11. * @ClassName CxfConfig
  12. * @Description: cxf 发布所有接口至服务
  13. * @Author wuchao
  14. * @Date 2020/1/13
  15. * @Version V1.0
  16. **/
  17. @Configuration
  18. public class CxfConfig {
  19. /*springboot 2.0.6版本之后写法改为配置文件*/
  20. /* @Bean
  21. public ServletRegistrationBean dispatcherServlet() {
  22. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/util/*");
  23. servletRegistrationBean.setName("GfService");
  24. return servletRegistrationBean;
  25. }*/
  26. @Bean(name = Bus.DEFAULT_BUS_ID)
  27. public SpringBus springBus() {
  28. return new SpringBus();
  29. }
  30. @Bean
  31. public GfService gfJsonService() {
  32. return new GfServiceImpl();
  33. }
  34. @Bean
  35. public Endpoint endpoint() {
  36. EndpointImpl endpoint = new EndpointImpl(springBus(), gfJsonService());
  37. endpoint.publish("/api");
  38. endpoint.getInInterceptors().add(new WsInterceptor());
  39. return endpoint;
  40. }
  41. }

 Springboot2.0.6 版本之后,  请在你的 bootstrap.yml 文件  在上这句话,并且注释 ServletRegistrationBean dispatcherServlet()方法, 请看上面 注释

  1. cxf:
  2. path: /GfService
  1. import org.apache.cxf.helpers.IOUtils;
  2. import org.apache.cxf.interceptor.AbstractLoggingInterceptor;
  3. import org.apache.cxf.interceptor.Fault;
  4. import org.apache.cxf.io.CachedOutputStream;
  5. import org.apache.cxf.io.DelegatingInputStream;
  6. import org.apache.cxf.message.Message;
  7. import org.apache.cxf.phase.Phase;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.SequenceInputStream;
  12. import java.util.logging.Logger;
  13. /**
  14. * @ClassName WsInterceptor
  15. * @Description: 过滤器
  16. * @Author wuchao
  17. * @Date 2020/1/13
  18. * @Version V1.0
  19. **/
  20. public class WsInterceptor extends AbstractLoggingInterceptor {
  21. private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(WsInterceptor.class);
  22. public WsInterceptor() {
  23. super(Phase.RECEIVE);
  24. }
  25. @Override
  26. protected Logger getLogger() {
  27. return null;
  28. }
  29. @Override
  30. public void handleMessage(Message message) throws Fault {
  31. InputStream is = message.getContent(InputStream.class);
  32. if (is != null) {
  33. CachedOutputStream bos = new CachedOutputStream();
  34. if (threshold > 0) {
  35. bos.setThreshold(threshold);
  36. }
  37. try {
  38. // 使用适当的输入流并在以后还原
  39. InputStream bis = is instanceof DelegatingInputStream ? ((DelegatingInputStream) is).getInputStream() : is;
  40. //仅复制到最大限制,因为这就是我们需要记录的全部
  41. //我们可以流剩下的
  42. IOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);
  43. bos.flush();
  44. bis = new SequenceInputStream(bos.getInputStream(), bis);
  45. // 恢复委托输入流或输入流
  46. if (is instanceof DelegatingInputStream) {
  47. ((DelegatingInputStream) is).setInputStream(bis);
  48. } else {
  49. message.setContent(InputStream.class, bis);
  50. }
  51. bos.close();
  52. } catch (IOException e) {
  53. throw new Fault(e);
  54. } finally {
  55. LOGGER.info(bos.toString());
  56. }
  57. }
  58. }
  59. }

 

2.4,接口

  1. /**
  2. * @ClassName gfService
  3. * @Description: TODO
  4. * @Author wuchao
  5. * @Date 2020/1/13
  6. * @Version V1.0
  7. * @WebService 使接口为webService接口
  8. * @name 暴露服务名称
  9. * @targetNamespace 命名空间, 一般是接口的包名倒序
  10. **/
  11. @WebService(name = "GfService", targetNamespace = "http://service.api.platform.techhero.com")
  12. public interface GfService {
  13. /**
  14. * 获取八大类型接口
  15. *
  16. * @param model
  17. * @param czsj
  18. * @param num
  19. * @return
  20. */
  21. @WebMethod
  22. String getBusinessData(@WebParam(name = "model") String model, @WebParam(name = "czsj") Date czsj, @WebParam(name = "num") int num);
  23. }
  1. import com.jfinal.plugin.activerecord.Db;
  2. import com.jfinal.plugin.activerecord.Record;
  3. import com.techhero.platform.api.entity.ModelXml;
  4. import com.techhero.platform.api.service.GfService;
  5. import com.techhero.platform.api.util.WebUtil;
  6. import javax.jws.WebService;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. /**
  12. * @ClassName GfServiceImpl
  13. * @Description: TODO
  14. * @Author wuchao
  15. * @Date 2020/1/13
  16. * @Version V1.0
  17. * @WebService 使接口为webService接口
  18. * @serviceName 与接口中指定的name一致
  19. * @targetNamespace 命名空间, 一般是接口的包名倒序
  20. * @endpointInterface 接口地址
  21. **/
  22. @WebService(serviceName ="GfService",targetNamespace="http://service.api.platform.techhero.com",
  23. endpointInterface="com.techhero.platform.api.service.GfService")
  24. public class GfServiceImpl implements GfService {
  25. /**
  26. * 获取八大类型接口
  27. *
  28. * @param model
  29. * @param czsj
  30. * @param num
  31. * @return
  32. */
  33. @Override
  34. public String getBusinessData(String model, Date czsj, int num) {
  35. /*此处业务逻辑*/
  36. return null;
  37. }
  38. }

2.4 ,客服端 

 

  1. import org.apache.cxf.endpoint.Client;
  2. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  3. import org.junit.Test;
  4. import javax.xml.datatype.DatatypeFactory;
  5. import javax.xml.datatype.XMLGregorianCalendar;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.GregorianCalendar;
  10. /**
  11. *@ClassName WebServerTest 客服端测试类
  12. *@Description: TODO
  13. *@Author wuchao
  14. *@Date 2020/1/13
  15. *@Version V1.0
  16. **/
  17. public class WebServiceTest {
  18. @Test
  19. public void testSend1(){
  20. // 创建动态客户端
  21. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
  22. Client client = dcf.createClient("http://localhost:9099/GfService/api?wsdl");
  23. // 需要密码的情况需要加上用户名和密码
  24. // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
  25. Object[] objects = new Object[0];
  26. try {
  27. //Date作为参数传递时需要转化为XMLGregorianCalendar类型
  28. Date date = parseDate("2019-08-01 09:28:00","yyyy-MM-dd HH:mm:ss");
  29. GregorianCalendar cal = new GregorianCalendar();
  30. cal.setTime(date);
  31. XMLGregorianCalendar xmlDate = null;
  32. try {
  33. xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. // invoke("方法名",参数1,参数2,参数3....);
  38. /*获取八大类型*/
  39. //objects = client.invoke("getBusinessData", "300002001",xmlDate,-1);
  40. /*获取年度目标值*/
  41. //objects = client.invoke("notifyYearData", "2019-04-01","1000","014043");
  42. /*获取月度目标值*/
  43. objects = client.invoke("getMonthData", "2019-04-01");
  44. System.out.println( objects[0]);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. public static Date parseDate(String dateStr, String parseStyle) {
  50. Date date = null;
  51. if (null != parseStyle && !"".equals(parseStyle.trim()) && null != dateStr && !"".equals(dateStr.trim())) {
  52. SimpleDateFormat sdf = new SimpleDateFormat(parseStyle);
  53. try {
  54. date = sdf.parse(dateStr);
  55. } catch (ParseException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. return date;
  60. }

2.5, 有问题,我们可以一起交流,qq770149701

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57697?site
推荐阅读
相关标签
  

闽ICP备14008679号