当前位置: 首页 > news >正文

国内新闻最近新闻今天做seo推广公司

国内新闻最近新闻今天,做seo推广公司,鲜花销售网站开发费用,做静态页面的网站SpringBoot整合WebService WebService是一个比较旧的远程调用通信框架#xff0c;现在企业项目中用的比较少#xff0c;因为它逐步被SpringCloud所取代#xff0c;它的优势就是能够跨语言平台通信#xff0c;所以还有点价值#xff0c;下面来看看如何在SpringBoot项目中使…SpringBoot整合WebService WebService是一个比较旧的远程调用通信框架现在企业项目中用的比较少因为它逐步被SpringCloud所取代它的优势就是能够跨语言平台通信所以还有点价值下面来看看如何在SpringBoot项目中使用WebService 我们模拟从WebService客户端发送请求给WebService服务端暴露的下载文件服务并获取服务端返回的文件保存到本地 环境 SpringBoot2.7.3 Jdk17 服务端 在SpringBoot中整合WebService的服务端需要通过一个配置文件将服务接口暴露出去给客户端调用 项目结构 配置 服务端POM ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdwebservice/artifactIdversion0.0.1-SNAPSHOT/versionnamewebservice/namedescriptionwebservice/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--cxf--dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion3.5.1/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion3.5.1/version/dependency!--hutool--dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.12/version/dependency!--fastjson--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.16/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project 服务端YML server: # 必须配置端口客户端需要port: 7001FileCxfConfig 该文件为WebService服务暴露配置文件 package com.example.webservice.config;import com.example.webservice.service.FileCxfService; import com.example.webservice.service.impl.FileCxfServiceImpl; 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.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;Configuration public class FileCxfConfig {Bean(name Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}Bean(name downloadFileBean)public ServletRegistrationBean dispatcherServlet() {ServletRegistrationBean wbsServlet new ServletRegistrationBean(new CXFServlet(), /file/*);return wbsServlet;}Beanpublic FileCxfService fileCxfService() {return new FileCxfServiceImpl();}Beanpublic Endpoint endpointPurchase(SpringBus springBus, FileCxfService fileCxfService) {EndpointImpl endpoint new EndpointImpl(springBus(), fileCxfService());endpoint.publish(/download);System.err.println(服务发布成功地址为http://localhost:7001/file/download?wsdl);return endpoint;}}FileCxfService 该类指定了暴露的服务接口注意类中的注解都很重要不能丢具体可以看说明 package com.example.webservice.service;import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.BindingType;BindingType(value http://www.w3.org/2003/05/soap/bindings/HTTP/) WebService(serviceName FileCxfService, // 与接口中指定的name一致targetNamespace http://webservice.example.com // 与接口中的命名空间一致,一般是接口的包名倒 ) public interface FileCxfService {WebMethod(operationName downloadFile)WebResult(name String)String downloadFile(WebParam(name params, targetNamespace http://webservice.example.com) String params,WebParam(name token, targetNamespace http://webservice.example.com) String token);} FileCxfServiceImpl 该类指定了暴露的服务接口的具体实现注意类中的注解都很重要不能丢具体可以看说明 package com.example.webservice.service.impl;import cn.hutool.core.codec.Base64; import com.alibaba.fastjson2.JSONObject; import com.example.webservice.pojo.FileDto; import com.example.webservice.service.FileCxfService;import javax.jws.WebService;WebService(serviceName FileCxfService, // 与接口中指定的name一致targetNamespace http://webservice.example.com // 与接口中的命名空间一致,一般是接口的包名倒 ) public class FileCxfServiceImpl implements FileCxfService {Overridepublic String downloadFile(String params, String token) {//下载文件System.err.println(params : params);FileDto fileDto JSONObject.parseObject(params, FileDto.class);System.err.println(fileDto : fileDto);String data null;try {data Base64.encode(C:\\Users\\YIQI\\Desktop\\ebook\\Java70.pdf);} catch (Exception e) {e.printStackTrace();}System.err.println(data);return data;}} FileDto 该类为参数实体类用于接受客户端传来的参数 package com.example.webservice.pojo;import lombok.AllArgsConstructor; import lombok.Data; import lombok.ToString;Data AllArgsConstructor ToString public class FileDto {private String fileId;private String newFileId;private String bucketName;} 客户端 在SpringBoot中整合WebService的客户端需要指定服务端暴露的服务接口 项目结构 配置 客户端POM ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdwebclient/artifactIdversion0.0.1-SNAPSHOT/versionnamewebclient/namedescriptionwebclient/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--cxf--dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion3.5.1/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion3.5.1/version/dependency!--hutool--dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.12/version/dependency!--fastjson--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.16/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project 客户端YML server: # 可以不配置port: 1000FileCxfService 这个文件和服务端的FileCxfService保持一致用于指定客户端请求的方式 package com.example.webclient.service;import javax.jws.WebParam; import javax.jws.WebService;WebService(name FileCxfService, // 暴露服务名称targetNamespace http://webservice.example.com// 命名空间,一般是接口的包名倒序 ) public interface FileCxfService {String downloadFile(WebParam(name data, targetNamespace http://webservice.example.com) String data,WebParam(name token, targetNamespace http://webservice.example.com) String token);} FileCxfClient 这个类是客户端的主类里面有发送WebService请求的方法 package com.example.webclient.client;import cn.hutool.core.codec.Base64; import com.alibaba.fastjson2.JSONObject; import com.example.webclient.pojo.FileDto; import com.example.webclient.service.FileCxfService; import com.example.webclient.util.ConvertBASE64;import javax.xml.bind.DatatypeConverter; import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL;public class FileCxfClient {public static void main(String[] args) throws Exception {// 创建wsdl的urlURL url new URL(http://localhost:7001/file/download?wsdl);// 指定命名空间和服务名称QName qName new QName(http://webservice.example.com, FileCxfService);Service service Service.create(url, qName);// 通过getPort方法返回指定接口FileCxfService myServer service.getPort(FileCxfService.class);// 调用方法返回数据FileDto fileDto new FileDto();fileDto.setFileId(1);fileDto.setNewFileId(1);fileDto.setBucketName(book);String params JSONObject.toJSONString(fileDto);Long st System.currentTimeMillis();String file myServer.downloadFile(params, TOKEN:ABC);// 解析文件到本地// 可以解析成字节数组如果服务端返回的也是字节数组的话byte[] decode Base64.decode(file);// 也可以将Base64写入到本地文件中ConvertBASE64.decoderBase64File(file, C:\\Users\\YIQI\\Desktop\\ebook\\demo70.pdf);System.err.println(decode : decode.toString());System.err.println(result get file success!);System.err.println(cost time : (System.currentTimeMillis() - st) / 1000 s.);}} FileDto 这个类是封装请求参数的实体类和服务端的FileDto保持一致 package com.example.webclient.pojo;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString;Data AllArgsConstructor NoArgsConstructor ToString public class FileDto {private String fileId;private String newFileId;private String bucketName;} ConvertBASE64 该类是Base64工具类可以完成Base64字符串和文件的互换 package com.example.webclient.util;import cn.hutool.core.codec.Base64Decoder; import cn.hutool.core.codec.Base64Encoder; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson2.JSONObject;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class ConvertBASE64 {/*** 将文件转成base64编码字符串** param path* return* throws Exception*/public static String encodeBase64File(String path) throws Exception {File file new File(path);FileInputStream inputFile new FileInputStream(file);byte[] buffer new byte[(int) file.length()];inputFile.read(buffer);inputFile.close();return new Base64Encoder().encode(buffer);}/*** 将base64编码字符串转成文件** param base64Code* param targetPath* throws Exception*/public static void decoderBase64File(String base64Code, String targetPath)throws Exception {byte[] buffer Base64Decoder.decode(base64Code);FileOutputStream out new FileOutputStream(targetPath);out.write(buffer);out.close();}/*** 将base64字节装成文件** param base64Code* param targetPath* throws Exception*/public static void toFile(String base64Code, String targetPath)throws Exception {byte[] buffer base64Code.getBytes();FileOutputStream out new FileOutputStream(targetPath);out.write(buffer);out.close();}public static String toJson(Object obj) {return JSONObject.toJSONString(obj);}public static Object toObject(String JSONString, Class cls) {return JSONObject.parseObject(JSONString, cls);}public static void writeByteArrayToFile(File desFile, byte[] data)throws IOException {FileUtil.writeBytes(data, desFile);}public static byte[] readFileToByteArray(File srcFile)throws IOException {return FileUtil.readBytes(srcFile);}public static String encode(String string) {return new String(Base64Encoder.encode(string.getBytes()));}public static void main(String[] args) {try {String a encodeBase64File(C:\\Users\\YIQI\\Desktop\\工作文件\\bg2.jpg);// String base64Code encodeBase64File(D:/0101-2011-qqqq.tif);System.out.println(a);decoderBase64File(a, C:\\Users\\YIQI\\Desktop\\工作文件\\bg3.jpg);// toFile(base64Code, D:\\three.txt);} catch (Exception e) {e.printStackTrace();}}} 测试 先启动服务端可以看到对外发布的服务 在启动客户端给服务端发送请求的方法可以看到服务端返回的数据 因为我把从服务端获取的文件写入到了本地所以可以在文件目录中看到该文件
http://www.hkea.cn/news/14536525/

相关文章:

  • 申请一个域名可以建设一个网站吗腾讯云网站备案
  • 乐清企业网站建设店铺推广渠道有哪些方式
  • 腾讯云怎样做网站中国住房和城乡建设部查询网站
  • pc 移动的网站开发网站滑动效果怎么做
  • 海南网站制作网站建设的技术准备
  • 阿里云网站开发服务器数字广东网络建设有限公司是国企吗
  • 有人用dw做网站吗网站开发是怎么样的
  • 问题不大工作室 网站网络建设费用
  • 优推宝可以做自己网站吗义乌 外贸网站 开发
  • 常见的搜索引擎有哪些博山专业网站优化哪家好
  • 怎样做公司的网站电子商务网站开发费用入账
  • 营销网站中最重要的部分是郑州同济医院妇科怎么样
  • 网站建设 ui设计网站系统参数设置
  • 邯郸网站建设公司排名环保局网站设计方案
  • 通州建设局网站网站兼容性代码
  • 建设网站装配式建筑楼房淮北官方网站
  • 建设项目环保备案登记网站网络营销成功案例
  • 淘宝客做网站备注怎么写的网站站点建设
  • 最好的免费软件网站建设四川做网站设计的公司
  • 手机端网站做app如何制作网页设计
  • 中国做外贸的网站有哪些企业网站用免费程序
  • 网页中网站设计规划流程wordpress设置爬虫页面
  • 中山精品网站建设咨询跨境外贸是做什么的
  • 福州网站建设要找嘉艺网络网站用途说明
  • 毕业设计代做网站 知乎网站建设公司上海做网站公司哪家好
  • 中国建设投资集团 网站首页网站通知做文献的格式
  • 万网怎么建设网站网站如何做提现功能
  • 企业在线设计网站做网站除了域名还用什么
  • 档案馆网站机房建设wordpress添加底部漂浮栏菜单
  • 做付费动漫网站WordPress管理员密码多少位