记录commandlinerunner错误解决

不研究原因只记录错
实现预先加载接口的时候commandlinerunner,运行后会报错failed to execute commandlinerunner;
所以加个for循环

@Component
@Order(value = 1)
public class YxRunner implements CommandLineRunner {

    @Value("${file.jtwjlj}")
    private String jtwjlj;


    @Autowired
    private ISourceService sourceService;



    @Override
    public void run(String... args) throws Exception {

        //这里加了个for循环,加上就不报错了
        int a = 10;
        for (int a1 = 0; a1 < a; a1++) {

            while (true) {
							
							//这里做处理流程即可

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }


        }


    }

}

package com.yuncheng.clinet; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.model.function.FunctionCallback; import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.ai.tool.ToolCallbackProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.event.EventListener; import org.springframework.web.client.RestTemplate; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.util.*; @SpringBootApplication public class McpClientApplication { public static void main(String[] args) { SpringApplication.run(McpClientApplication.class, args); } @Bean public CommandLineRunner predefinedQuestions(OpenAiChatModel openAiChatModel, ToolCallbackProvider tools, ConfigurableApplicationContext context) { System.out.println(">>> 实际注册的工具名=" + Arrays.stream(tools.getToolCallbacks()) .map(FunctionCallback::getName).toList()); /* ===== 统一打印真实 HTTP 信息(仅注册一次) ===== */ RestTemplate rt = getRestTemplate(openAiChatModel); if (rt != null) { rt.getInterceptors().add((request, body, execution) -> { System.out.println("\n>>> 真实请求URL: " + request.getURI()); System.out.println(">>> 真实请求头: " + request.getHeaders()); System.out.println(">>> 真实请求体: " + new String(body, StandardCharsets.UTF_8)); return execution.execute(request, body); }); } /* ===================================================== */ return args -> { List<String> allow = List.of("getWeatherByCityUnique"); Set<String> set = new HashSet<>(); List<FunctionCallback> filtered = Arrays.stream(tools.getToolCallbacks()) .filter(tc -> allow.contains(tc.getName()) && set.add(tc.getName())) .toList(); ChatClient chatClient = ChatClient.builder(openAiChatModel) .defaultTools(filtered) .build(); Scanner scanner = new Scanner(System.in); System.out.println(">>> 欢迎使用问答系统!输入'exit'退出程序。"); while (true) { System.out.print("\n>>> QUESTION: "); String userInput = scanner.nextLine(); if ("exit".equalsIgnoreCase(userInput)) { System.out.println(">>> 已退出问答系统。"); break; } try { String reply = chatClient.prompt(userInput).call().content(); System.out.println("\n>>> ASSISTANT: " + reply); } catch (Exception e) { System.out.println("\n>>> ERROR: 无法处理您的请求,请稍后再试。"); e.printStackTrace(); } } context.close(); scanner.close(); }; } /* 纯反射拿到 RestTemplate,无需 spring-test */ private static RestTemplate getRestTemplate(OpenAiChatModel chatModel) { try { Field apiField = chatModel.getClass().getDeclaredField("openAiApi"); apiField.setAccessible(true); Object api = apiField.get(chatModel); Field rtField = api.getClass().getDeclaredField("restTemplate"); rtField.setAccessible(true); return (RestTemplate) rtField.get(api); } catch (Exception e) { System.out.println(">>> 警告:获取 RestTemplate 失败,无法打印 HTTP 详情"); return null; } } } 代码是这个
最新发布
10-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值