langchain4j整合springboot
1.搭建项目架子
配置文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yd</groupId>
<artifactId>langchain4j_springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>langchain4j_springboot</name>
<description>langchain4j_springboot</description>
<properties>
<java.version>17</java.version>
<!-- -->
<langchain4j.version>1.0.0-beta1</langchain4j.version>
</properties>
<dependencies>
<!-- -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<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>
<scope>test</scope>
</dependency>
</dependencies>
<!-- -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>${langchain4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 9090
langchain4j:
community:
dashscope:
chat-model:
api-key: xxx
model: qwen-max
Controller测试
package com.yd.langchain4j_springboot;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("ai")
public class ChatController {
@Autowired
QwenChatModel qwenChatModel;
@RequestMapping("/chat")
public String test(@RequestParam(defaultValue="你是谁") String message){
String chat=qwenChatModel.chat(message);
return chat;
}
@RequestMapping("/a")
public String test2(){
String chat="a";
return chat;
}
}
测试结果
补充:
- 阿里云api注册以及使用:官网
- 两个报错
网页漏看路径:Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Jun 16 23:09:14 CST 2025 There was an unexpected error (type=Not Found, status=404).
环境配置版本问题 - Consider defining a bean of type 'dev.langchain4j.community.model.dashscope.QwenChatModel
- dev.langchain4j:langchain4j-community-dashscope-spring-boot-starter:jar:unknown was not found in https://maven.aliyun.com/nexus/content/groups/public/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of alimaven has elapsed or updates are forced