引言
在当今的 AIGC(人工智能生成内容)领域,ComfyUI 凭借其强大的绘图能力受到广泛关注。为了在我们的程序中通过 API 与 ComfyUI 进行通信,我们可以借助 Spring Boot 和 Retrofit 这两个强大的工具。本文将详细介绍如何创建一个 Spring Boot 项目,并使用 Retrofit 框架来调用 ComfyUI 的 API,同时会讲解如何配置项目依赖、YML 文件以及如何进行测试。
一、创建项目
1.1 项目初始化
首先,我们要创建一个 Spring Boot 工程,命名为star-graph,并将项目 JDK 设置为 17 版本。这里有一个基础工程可供直接使用,路径为AIGC应用与智能体开发\02讲义资料\第二章\代码\Retrofit入门基础工程。
1.2 设置项目 Pom 依赖
项目基于 Spring Boot 3.2 版本构建,引入了 Redis、Mysql、Hutool、WebSocket 等依赖。以下是pom.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>star-graph</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis_plus.version>3.5.7</mybatis_plus.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.8</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.28</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.31</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis_plus.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.30.0</version>
</dependency>
</dependencies>
<build>
<finalName>star-graph-demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
将上述内容覆盖新建项目的pom.xml文件,然后刷新 Maven 依赖。
1.3 配置项目 Yml 文件
由于项目依赖了 Mysql 和 Redis,需要在application.yml文件中配置相关属性信息:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.100.129:3306/star_graph?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: root
password: 87sdhf298TYUUIz2!
data:
redis:
host: 192.168.100.129
port: 6379
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
table-prefix: sg_
logging:
level:
root: info
1.4 创建启动类并启动测试
创建启动类StarGraphApp:
package cn.itcast.star.graph;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StarGraphApp {
public static void main(String[] args) {
SpringApplication.run(StarGraphApp.class, args);
}
}
运行该启动类,如果启动过程未出现错误,则项目创建成功。
二、Retrofit 客户端
2.1 Retrofit 介绍
Retrofit 是基于 OkHttp 开发的、类型安全的 Android 或 Java Http 框架。它基于 OkHttp 封装,性能好,同时融入了流行的声明式开发思想,便于开发和学习。官网地址为:Retrofit
2.2 Retrofit 入门
2.2.1 声明 API
创建接口类ComfyuiApi:
package cn.itcast.star.graph.comfyui.client.api;
import retrofit2.Call;
import retrofit2.http.GET;
import java.util.HashMap;
public interface ComfyuiApi {
/**
* 获取系统信息
* @return
*/
@GET("/system_stats")
Call<HashMap> getSystemStats();
}
在这个接口中,我们定义了一个方法getSystemStats,通过@GET注解声明请求方式为 GET,请求地址为/system_stats,返回结果为Call<HashMap>对象。
2.2.2 生效 API
创建配置类ComfyuiConfig:
package cn.itcast.star.graph.comfyui.client.config;
import cn.itcast.star.graph.comfyui.client.api.ComfyuiApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.io.IOException;
@Configuration
public class ComfyuiConfig {
@Bean
public ComfyuiApi comfyuiApi() throws IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.100.129:8188")
.addConverterFactory(JacksonConverterFactory.create())
.build();
ComfyuiApi comfyuiApi = retrofit.create(ComfyuiApi.class);
return comfyuiApi;
}
}
在这个类中,我们通过Retrofit.Builder构建一个 Retrofit 客户端,指定请求的服务器地址和请求数据的转换器,最后调用retrofit.create方法创建ComfyuiApi接口的实现。
2.2.3 测试 API
创建测试类ComfyuiApiTest:
package cn.itcast.star.graph;
import cn.itcast.star.graph.comfyui.client.api.ComfyuiApi;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ComfyuiApiTest {
@Autowired
ComfyuiApi comfyuiApi;
@Test
public void test() throws Exception {
System.out.println(comfyuiApi.getSystemStats().execute().body());
}
}
在这个类中,我们直接注入ComfyuiApi,并调用getSystemStats()方法发起请求,打印出结果。
总结
通过以上步骤,我们成功创建了一个 Spring Boot 项目,并使用 Retrofit 框架实现了与 ComfyUI 的通信。Retrofit 的声明式开发思想让我们的代码更加简洁易读,同时 Spring Boot 的便捷性也大大提高了开发效率。希望本文能对大家在 AIGC 绘图案例开发中有所帮助。


2521

被折叠的 条评论
为什么被折叠?



