首页 > 其他分享 >springboot 整合webservice 相关说明

springboot 整合webservice 相关说明

时间:2023-05-16 11:12:57浏览次数:44  
标签:info account finance String generated 整合 webservice public springboot

1.环境依赖 jdk8, springboot 2.3.12.release,cxf版本需要根据springboot版本修改,方法:查看springboot版本的发布日期,然后根据日期找相近的两个版本

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>

<dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.4.5</version>
</dependency>

2. WebserviceConfig.java

 1 package com.example.demo.demos.nacosconfig;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 import org.apache.cxf.bus.spring.SpringBus;
 6 import org.apache.cxf.jaxws.EndpointImpl;
 7 import org.apache.cxf.transport.servlet.CXFServlet;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.web.servlet.ServletRegistrationBean;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 
13 import com.example.demo.demos.webservice.IUserWebService;
14 
15 @Configuration
16 public class WebServiceConfig {
17 
18   /************* 要发布的webservice,需要有对应的实现类,建议参照下文中的命名规范 ****************/
19     @Autowired
20     private IUserWebService webService;
21 
22     @SuppressWarnings({ "rawtypes", "unchecked" })
23     @Bean
24     public ServletRegistrationBean cxfServlet() {
25         return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
26     }
27 
28     @Bean
29     public SpringBus cxf() {
30         return new SpringBus();
31     }
32 
33     @Bean
34     public Endpoint endpoint() {
35         EndpointImpl endpoint = new EndpointImpl(cxf(), webService);
36         endpoint.publish("/api");
37         return endpoint;
38     }
39 }
View Code

3. 接口示例和规范

 1 package com.example.demo.demos.webservice;
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 
 6 /**
 7  * name决定了生成的相关调用代码是否简单明了且符合上下文语境<br/>
 8  * targetNamespace 无所谓,和实现类一致即可
 9  * @author Administrator
10  *
11  */
12 @WebService(name = "userService", targetNamespace = "https://www.demo.tech")
13 public interface IUserWebService {
14 
15     @WebMethod
16     public String sayHello(String name);
17 }
View Code

4.实现类

 1 package com.example.demo.demos.webservice.impl;
 2 
 3 import javax.jws.WebService;
 4 
 5 import org.springframework.stereotype.Component;
 6 
 7 import com.example.demo.demos.webservice.IUserWebService;
 8 
 9 /**
10  * 
11  * class 类名如下,去掉接口中的首字母'I'和结尾单词'Service'<br/>
12  * 生成的调用代码语境就比较通顺
13  * 
14  * @author Administrator
15  *
16  */
17 @Component
18 @WebService(targetNamespace = "https://www.demo.tech", endpointInterface = "com.example.demo.demos.webservice.IUserWebService")
19 public class UserWeb implements IUserWebService {
20 
21     public String sayHello(String name) {
22         return String.format("hello,%s", name);
23     }
24 }
View Code

5. 打开要生成的文件夹,即生成的代码要保存到哪里就打开哪个文件夹,然后运行命令 wsimport  -encoding UTF-8 -s . http://127.0.0.1:8080/ws/api?wsdl 自动生成java相关代码

6. 测试代码就会变得语境通顺

 1 package com.example.demo;
 2 
 3 import java.net.MalformedURLException;
 4 import java.net.URL;
 5 
 6 import javax.xml.namespace.QName;
 7 import javax.xml.ws.Service;
 8 
 9 import https.www_zqxx.UserService;
10 
11 public class WebServiceTest {
12     public static void main(String[] args) throws MalformedURLException {
13         System.setProperty("javax.xml.bind.JAXBContext", "com.sun.xml.internal.bind.v2.ContextFactory");
14         // 创建WSDL的URL
15         URL url = new URL("http://localhost:8080/ws/api?wsdl");
16         // 指定命名空间和服务名称
17         QName qName = new QName("https://www.demo.tech", "UserWebService");
18         Service service = Service.create(url, qName);
19         // 通过getPort方法返回指定接口
20         UserService userService = service.getPort(UserService.class);
21         String res = userService.sayHello("小明");
22         System.out.println(res);
23     }
24 }
View Code

 7. 统一封装结果类

package com.zqxxjs.v1.common;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "ResponseEntity")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "code", "msg", "result" })
public class ResponseEntity<T> {

    private Integer code;
    private String msg;

    private Message<T> result;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Message<T> getResult() {
        return result;
    }

    public void setResult(Message<T> result) {
        this.result = result;
    }

    public static <E> ResponseEntity<E> success(List<E> data, Long count) {
        ResponseEntity<E> entity = new ResponseEntity<>();
        entity.setCode(Constant.SUCCESS_CODE);
        entity.setMsg(Constant.SUCCESS_MSG);
        Message<E> resultTmp = new Message<>();
        resultTmp.setCount(count);
        resultTmp.setData(data);
        entity.setResult(resultTmp);
        return entity;
    }

}
View Code

8. 配合mbg插件,在生成的实体类上边要加的相关注解,注解生成工具:

package com.zqxxjs.order.common;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class XmlUtil {
    public static void main(String[] args) throws Exception {
        String packageName="com.zqxxjs.order.entity";
        String path = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/")).getPath();
        Collection<File> entityClassFile = FileUtils.listFiles(new File(path), new String[] { "class" }, false);
        for (File file : entityClassFile) {
            if (file.getName().contains("Example")) {
                continue;
            }
            Class<?> c = Class.forName(packageName+"." + FilenameUtils.getBaseName(file.getName()));
            Field[] fds = c.getDeclaredFields();
            String s = ("@XmlType(propOrder = { ");
            String[] tmp = new String[fds.length - 1];
            for (int i = 0; i < tmp.length; i++) {
                Field f = fds[i];
                if (f.getName().equalsIgnoreCase("serialVersionUID")) {
                    continue;
                }

                tmp[i] = String.format("\"%s\"", f.getName());
            }
            
            
            System.out.println("\n\n@XmlRootElement(name = \""+c.getSimpleName()+"\")");
            System.out.println("@XmlAccessorType(XmlAccessType.FIELD)");
            s = s.concat(String.join(",", tmp)).concat("})");
            System.out.println(s);
            System.out.println("-----------------------------------------------------------------");
        }

    }
}
View Code

9. 加好后的示例代码

package com.zqxxjs.v1.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 *
 * This class was generated by MyBatis Generator. This class corresponds to the
 * database table finance_account_info
 */

@XmlRootElement(name = "FinanceAccountInfo")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "id", "serialNumber", "projectNumber", "projectName", "director", "amountOfMoney", "createDate",
        "financeNumber", "remark" })
public class FinanceAccountInfo implements Serializable {
    /**
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.id
     *
     * @mbg.generated
     */
    private Integer id;

    /**
     * Database Column Remarks: 流水号
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.serial_number
     *
     * @mbg.generated
     */
    private String serialNumber;

    /**
     * Database Column Remarks: 项目编号
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.project_number
     *
     * @mbg.generated
     */
    private String projectNumber;

    /**
     * Database Column Remarks: 项目名称
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.project_name
     *
     * @mbg.generated
     */
    private String projectName;

    /**
     * Database Column Remarks: 负责人
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.director
     *
     * @mbg.generated
     */
    private String director;

    /**
     * Database Column Remarks: 金额
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.amount_of_money
     *
     * @mbg.generated
     */
    private BigDecimal amountOfMoney;

    /**
     * Database Column Remarks: 往来日期
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.create_date
     *
     * @mbg.generated
     */
    private String createDate;

    /**
     * Database Column Remarks: 财务编号
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.finance_number
     *
     * @mbg.generated
     */
    private String financeNumber;

    /**
     * Database Column Remarks: 备注
     *
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database column finance_account_info.remark
     *
     * @mbg.generated
     */
    private String remark;

    /**
     * This field was generated by MyBatis Generator. This field corresponds to the
     * database table finance_account_info
     *
     * @mbg.generated
     */
    private static final long serialVersionUID = 1L;

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.id
     *
     * @return the value of finance_account_info.id
     *
     * @mbg.generated
     */
    public Integer getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.id
     *
     * @param id the value for finance_account_info.id
     *
     * @mbg.generated
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.serial_number
     *
     * @return the value of finance_account_info.serial_number
     *
     * @mbg.generated
     */
    public String getSerialNumber() {
        return serialNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.serial_number
     *
     * @param serialNumber the value for finance_account_info.serial_number
     *
     * @mbg.generated
     */
    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.project_number
     *
     * @return the value of finance_account_info.project_number
     *
     * @mbg.generated
     */
    public String getProjectNumber() {
        return projectNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.project_number
     *
     * @param projectNumber the value for finance_account_info.project_number
     *
     * @mbg.generated
     */
    public void setProjectNumber(String projectNumber) {
        this.projectNumber = projectNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.project_name
     *
     * @return the value of finance_account_info.project_name
     *
     * @mbg.generated
     */
    public String getProjectName() {
        return projectName;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.project_name
     *
     * @param projectName the value for finance_account_info.project_name
     *
     * @mbg.generated
     */
    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.director
     *
     * @return the value of finance_account_info.director
     *
     * @mbg.generated
     */
    public String getDirector() {
        return director;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.director
     *
     * @param director the value for finance_account_info.director
     *
     * @mbg.generated
     */
    public void setDirector(String director) {
        this.director = director;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.amount_of_money
     *
     * @return the value of finance_account_info.amount_of_money
     *
     * @mbg.generated
     */
    public BigDecimal getAmountOfMoney() {
        return amountOfMoney;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.amount_of_money
     *
     * @param amountOfMoney the value for finance_account_info.amount_of_money
     *
     * @mbg.generated
     */
    public void setAmountOfMoney(BigDecimal amountOfMoney) {
        this.amountOfMoney = amountOfMoney;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.create_date
     *
     * @return the value of finance_account_info.create_date
     *
     * @mbg.generated
     */
    public String getCreateDate() {
        return createDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.create_date
     *
     * @param createDate the value for finance_account_info.create_date
     *
     * @mbg.generated
     */
    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.finance_number
     *
     * @return the value of finance_account_info.finance_number
     *
     * @mbg.generated
     */
    public String getFinanceNumber() {
        return financeNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.finance_number
     *
     * @param financeNumber the value for finance_account_info.finance_number
     *
     * @mbg.generated
     */
    public void setFinanceNumber(String financeNumber) {
        this.financeNumber = financeNumber;
    }

    /**
     * This method was generated by MyBatis Generator. This method returns the value
     * of the database column finance_account_info.remark
     *
     * @return the value of finance_account_info.remark
     *
     * @mbg.generated
     */
    public String getRemark() {
        return remark;
    }

    /**
     * This method was generated by MyBatis Generator. This method sets the value of
     * the database column finance_account_info.remark
     *
     * @param remark the value for finance_account_info.remark
     *
     * @mbg.generated
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }
}
View Code

 

标签:info,account,finance,String,generated,整合,webservice,public,springboot
From: https://www.cnblogs.com/swtjavaspace/p/17404218.html

相关文章

  • AI绘画Stable Diffusion整合包V4三分钟超快速入门!
    本次整合包升级内容StableDiffusion整合包V4下载-torch2、xformers0.0.17、cudnn8.8打开无需任何操作即可满速(包括40系显卡-升级其它各种依赖版本-预置了Tagger(图反推关键词)的模型-预置了ControlNet、MultiDiffusion插件-优化了一些其他设置整合包只是打包了运行必须的py......
  • springboot(8)--定制服务
    springboot的服务配置除了application.properties,还可以通过implements WebServerFactoryCustomizer<T>定制服务,例如指定容器,端口,协议等等我们只要在继承类中添加自己的配置即可*@ClassnameTomcatServerConfiger*@CreatedbyMichael*@Date2023/5/15*@Descriptio......
  • SpringBoot发布https服务
    一、生成SSL证书 1、进入本地jdk的路径cdD:\Program\jdk1.8.0_77\jre\lib\securitycmd窗口生成证书HSoftTiger.keystore到D盘keytool-genkey-aliastigerCompany-keyalgRSA-keysize1024-keypasstigerpass-validity3650-keystoreD:\HSoftTiger.keystore-storep......
  • Microsoft.Exchange.WebServices.Data;
    using Microsoft.Exchange.WebServices.Data;using Microsoft.Identity.Client;using System;using System.Configuration; namespace EwsOAuth{   class Program  {     static async System.Threading.Tasks.Task Main(string[] args) ......
  • SpringBoot 文件上传到指定文件夹
    1、在application.yml文件中设置存储文件夹地址upload:path:E:\upload\2、配置映射路径@ConfigurationpublicclassWebMvcConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){//其......
  • SpringBoot 使用 Sa-Token 完成注解鉴权功能
    注解鉴权——优雅的将鉴权与业务代码分离。本篇我们将介绍在Sa-Token中如何通过注解完成权限校验。Sa-Token是一个轻量级java权限认证框架,主要解决登录认证、权限认证、单点登录、OAuth2、微服务网关鉴权等一系列权限相关问题。Gitee开源地址:https://gitee.com/dromar......
  • SpringBoot 如何进行限流?
    1.概述联网系统通常都要面对大并发大流量的请求,在突发情况下(最常见的场景就是秒杀、抢购),瞬时大流量会直接将系统打垮,无法对外提供服务。为了防止出现这种情况最常见的解决方案之一就是限流,当请求达到一定的并发数或速率,就进行等待、排队、降级、拒绝服务等。限流是对某一时间窗......
  • SpringBoot2原理篇-黑马
     原理篇1自动配置1.1bean的加载方式【一】1自动配置1.1bean的加载方式【一】1.1.1环境准备创建一个新的工程模块【Maven的,不是SpringBoot的】 直接创建 一个全新的Maven工程【添加坐标】<dependencies><dependency><groupId>org.spring......
  • 【SpringBoot】【配置加载】 SpringBoot配置加载解析时机原理
    1 前言不知道大家对于配置文件的加载有没有考虑过是什么时候加载解析的,这节我们就来看看。2 执行入口我们就先来看看加载配置的入口,核心类就是 ConfigFileApplicationListener主要作用就是读取应用的配置文件并add到Environment的PropertySources列表里。那么实际的执行过......
  • 【转载】SpringBoot自带的工具类
    断言对象、数组、集合ObjectUtilsStringUtilsCollectionUtils文件、资源、IO流FileCopyUtilsResourceUtilsStreamUtils反射、AOPReflectionUtils[AopUtils][AopContext]最近发现同事写了不少重复的工具类,发现其中很多功能,Spring自带的都有。于是整......