springboot整合swagger,jpa遇到的问题

发布于:2024-05-16 ⋅ 阅读:(69) ⋅ 点赞:(0)

1.整合jpa,版本问题导致Archive for required library: ‘C:/Users/Administrator/.m2/repository/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar’ in project ‘money-server’ cannot be read or is not a valid ZIP file money-server Build path Build Path Problem
解决方法,把spring-boot-starter-parent从1.5.3改为2.4.4新版本解决。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4</version>
</parent>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver # JDBC驱动类
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai # 数据库url
    username: root # 数据库用户名
    password: root # 数据库密码
  jpa:
    database: mysql # 数据库类型
    database-platform: org.hibernate.dialect.MySQL57Dialect # 数据库方言
    show-sql: true # 控制台打印SQL
    hibernate:
      ddl-auto: create # 根据实体类自动建表

2.整合swagger遇到guava找不到问题,
解决方法:配置文件加如下,给guava确定最新的稳定版本

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.0.1-jre</version>  <!--请使用最新的稳定版本 -->
    </dependency>

3.引入lombok不起作用,
解决办法:
1.配置文件加如下代码

  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>

2.eclipse的安装路径下找到eclipse.ini,最末尾加如下代码
-Xbootclasspath/a:C:/Users/Administrator/eclipse/java-2023-12/eclipse/lombok-1.18.30.jar
-javaagent:C:/Users/Administrator/eclipse/java-2023-12/eclipse/lombok-1.18.30.jar

4.返回给前台的数据封装到结果集

package com.gxl.money.util;


import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.Setter;

@ApiModel("Result 模型")
@Getter
@Setter
public class Result<T> implements Serializable{
    
    private Integer code;
    private String msg;
    private T data;
    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setCode(1);
        result.setMsg("成功");
        return result;
    }
 
    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.setData(object);
        result.setCode(1);
        result.setMsg("成功");
        return result;
    }
 
    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(0);
        return 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 T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

    
}

swagger和jpa同时整合,会各种冲突,目前正在单个项目测试击破,有问题再收集。