You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring DSL中无法转换Exchange Body为自定义对象,获取AccessToken遇空值

问题:Spring DSL中JWT响应转自定义对象返回null的解决方法

核心原因

Apache Camel不会自动将JSON格式的响应体转换为自定义Java对象,必须明确配置JSON转换器(如Jackson),同时保证字段映射逻辑正确,否则会出现转换后对象为null的情况。

具体解决步骤

1. 引入Jackson依赖

确保项目中已添加Camel-Jackson组件依赖(以Maven为例):

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jackson</artifactId>
    <version>你的Camel版本</version>
</dependency>

2. 配置Jackson数据格式转换器

在Spring配置中注册用于转换TokenResponse的JacksonDataFormat Bean:

@Bean
public JacksonDataFormat tokenResponseDataFormat() {
    return new JacksonDataFormat(TokenResponse.class);
}

如果使用XML配置:

<bean id="tokenResponseDataFormat" class="org.apache.camel.component.jackson.JacksonDataFormat">
    <property name="unmarshalType" value="你的包路径.TokenResponse"/>
</bean>

3. 在路由中添加Unmarshal步骤

调用OAuth端点后,增加unmarshal节点将JSON响应转为TokenResponse对象:

<to uri="ref:oauthTokenEndpoint" />
<log message="Auth Token Response is ${body} " loggingLevel="INFO" />
<unmarshal ref="tokenResponseDataFormat"/> <!-- 新增该节点 -->
<choice>
    <when>
        <simple>${header.CamelHttpResponseCode} == '200'</simple>
        <to uri="bean:getResponseCode?method=processOAuthToken" />
        <to uri="direct:sendEventPostServiceCall" />
    </when>
    <otherwise>
        <log message="[POST] Error getting token response code is ${header.CamelHttpResponseCode} " loggingLevel="INFO" />
    </otherwise>
</choice>

4. 优化TokenResponse类的字段映射

为了确保JSON下划线字段与Java属性正确映射,添加@JsonProperty注解(更稳妥,符合Java规范):

import com.fasterxml.jackson.annotation.JsonProperty;

public class TokenResponse {
    @JsonProperty("token_type")
    String tokenType;
    @JsonProperty("expires_in")
    String expiresIn;
    @JsonProperty("ext_expires_in")
    String extExpiresIn;
    @JsonProperty("access_token")
    String accessToken;
    
    // 驼峰命名的getter/setter
    public String getTokenType() {
        return tokenType;
    }
    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }
    public String getExpiresIn() {
        return expiresIn;
    }
    public void setExpiresIn(String expiresIn) {
        this.expiresIn = expiresIn;
    }
    public String getExtExpiresIn() {
        return extExpiresIn;
    }
    public void setExtExpiresIn(String extExpiresIn) {
        this.extExpiresIn = extExpiresIn;
    }
    public String getAccessToken() {
        return accessToken;
    }
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
}

5. 增加空值安全校验

在处理方法中添加空判断,避免空指针异常:

TokenResponse tokenResponse = exchange.getIn().getBody(TokenResponse.class);
if (tokenResponse != null && tokenResponse.getAccessToken() != null) {
    exchange.getOut().setHeader("jwt", tokenResponse.getAccessToken());
} else {
    logger.warn("TokenResponse对象为空或access_token字段缺失");
    // 可添加异常处理或降级逻辑
}

内容的提问来源于stack exchange,提问作者ravthiru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 06:15:02