Struts 1 Cannot retrieve mapping for action /* 解决办法

因为设置了mapping的返回,但返回的映射路径错了

 

解决办法

 

查找 struts-config.xml 文件里面没有对应上action和mapping的映射,检查 path="" 是否错误

<think>我们使用泛型类 `TycRes<T>` 来替代原来的 `ImageResponse`。假设 `TycRes` 的结构如下(根据实际响应结构调整): ```java public class TycRes<T> { private int error_code; private String reason; private T result; // 泛型字段,用于存放结果数据 // 省略getter和setter } ``` 在第一次请求中,我们期望返回的 `result` 是一个字符串(即图片URL),所以我们可以将 `TycRes<String>` 作为响应类型。 修改后的链式调用代码如下: ```java import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; public class ImageService { private final WebClient webClient; public ImageService(WebClient.Builder webClientBuilder) { this.webClient = webClientBuilder.baseUrl("http://open.api.tianyancha.com").build(); } public Mono<byte[]> fetchImageData() { // 第一次请求:获取包含URL的响应 return webClient.get() .uri("/services/open/ic/snapshot?keyword=北京百度网讯科技有限公司") .retrieve() .bodyToMono(new ParameterizedTypeReference<TycRes<String>>() {}) // 使用ParameterizedTypeReference处理泛型 .flatMap(tycRes -> { // 检查错误码,非0表示错误 if (tycRes.getError_code() != 0) { return Mono.error(new RuntimeException("API error: " + tycRes.getReason())); } String imageUrl = tycRes.getResult(); // 获取图片URL // 第二次请求:获取图片数据 return webClient.get() .uri(imageUrl) .retrieve() .bodyToMono(byte[].class); }); } } ``` ### 关键点说明: 1. **泛型类型处理**: 由于泛型在运行时会被擦除,我们需要使用 `ParameterizedTypeReference` 来保留泛型信息。这样,Jackson 才能正确地将 `result` 字段解析为 `String` 类型。 2. **错误处理**: 在第一次请求后,我们检查 `error_code` 是否为0。如果不是0,则通过 `Mono.error` 抛出异常,中断流并传递错误。 3. **链式调用**: 使用 `flatMap` 将第一次请求的结果映射到第二次请求。注意,如果第一次请求返回错误,则不会执行第二次请求。 4. **WebClient 配置**: 通过构造函数注入 `WebClient.Builder` 来创建 `WebClient` 实例,这是 Spring 推荐的注入方式,便于统一配置和测试。 ### 增强健壮性: - **超时设置**:可以为每个请求添加超时控制,例如: ```java .bodyToMono(new ParameterizedTypeReference<TycRes<String>>() {}) .timeout(Duration.ofSeconds(5)) // 设置超时 ``` - **重试机制**:对于可重试的错误(如网络波动),可以添加重试逻辑: ```java .retryWhen(Retry.backoff(3, Duration.ofMillis(100))) ``` ### 相关问题 1. 如何处理 WebClient 请求中的 HTTP 错误状态码(如 404/500)? 2. 如何为 WebClient 设置全局超时和重试机制? 3. 在 Spring Boot 中如何正确注入并配置 WebClient Bean? 4. 如何用 WebClient 实现多个并行请求并聚合结果? 5. 如何通过 WebClient 下载大文件并避免内存溢出?</think>### 使用泛型类 `TycRes` 实现 WebClient 非阻塞链式调用 要将 `ImageResponse` 替换为泛型类 `TycRes` 并保持功能正常,需通过 `ParameterizedTypeReference` 保留泛型信息,并正确处理响应结构。以下是完整实现方案: ```java import org.springframework.core.ParameterizedTypeReference; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; // 泛型响应类定义 public class TycRes<T> { private int error_code; private String reason; private T result; // 泛型结果字段 // getter/setter省略 } public class ImageService { private final WebClient webClient = WebClient.builder().build(); public Mono<byte[]> fetchImageData() { return webClient.get() .uri("http://open.api.tianyancha.com/services/open/ic/snapshot?keyword=北京百度网讯科技有限公司") .retrieve() // 关键:使用ParameterizedTypeReference保留泛型信息 .bodyToMono(new ParameterizedTypeReference<TycRes<String>>() {}) .flatMap(tycRes -> { // 1. 检查错误码 if (tycRes.getError_code() != 0) { return Mono.error(new RuntimeException("API error: " + tycRes.getReason())); } // 2. 获取result字段的值 String imageUrl = tycRes.getResult(); // 3. 发起二次请求 return webClient.get() .uri(imageUrl) .retrieve() .bodyToMono(byte[].class); }); } } ``` ### 关键实现说明 1. **泛型类型处理** 使用 `ParameterizedTypeReference` 解决泛型擦除问题: ```java // 明确指定TycRes<String>类型 .bodyToMono(new ParameterizedTypeReference<TycRes<String>>() {}) ``` 这确保 `result` 字段能正确解析为 `String` 类型[^1]。 2. **错误处理增强** 添加业务错误码检查: ```java if (tycRes.getError_code() != 0) { return Mono.error(...); // 中断链式调用 } ``` 3. **动态URL处理** 直接使用泛型类中的字段: ```java String imageUrl = tycRes.getResult(); // 自动识别为String类型 ``` 4. **响应结构调整** 根据天眼查API文档,`TycRes` 的结构定义为: ```java public class TycRes<T> { private int error_code; // 0表示成功 private String reason; // 错误信息 private T result; // 泛型结果字段 } ``` ### 执行流程 ```mermaid sequenceDiagram participant Client as 调用方 participant Service as ImageService participant API_A as 天眼查API participant API_B as 图片服务 Client->>Service: fetchImageData() // 发起请求 Service->>API_A: GET /snapshot?keyword=... API_A-->>Service: TycRes<String>(result=URL) Service->>API_B: GET [动态URL] API_B-->>Service: byte[](图片数据) Service-->>Client: 返回图片数据 ``` ### 生产环境优化建议 1. **配置超时控制** ```java .bodyToMono(new ParameterizedTypeReference<TycRes<String>>() {}) .timeout(Duration.ofSeconds(5)) // 添加超时控制 ``` 2. **重试机制** ```java .retryWhen(Retry.backoff(3, Duration.ofMillis(100))) ``` 3. **连接池配置** ```java HttpClient.create(ConnectionProvider.builder("pool") .maxConnections(50) .build()) ``` > **注意**:如果 `result` 字段是复杂对象而非字符串,只需修改泛型类型: > ```java > // 示例:result为自定义对象时 > .bodyToMono(new ParameterizedTypeReference<TycRes<CustomData>>() {}) > ``` --- ### 相关问题 1. 如何处理 WebClient 请求中的 HTTP 错误状态码(如 404/500)? 2. 如何为 WebClient 设置全局超时和重试机制? 3. 在 Spring Boot 中如何正确注入并配置 WebClient Bean? 4. 如何用 WebClient 实现多个并行请求并聚合结果? 5. 如何通过 WebClient 下载大文件并避免内存溢出?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值