如何将实现IEnumerable的复杂类型传递给请求正文中的C#Web API Post方法

问题描述

我正在尝试将JSON传递给请求正文中的C#Web API Post方法。将JSON反序列化为复杂类型(UserActionLogger)可以正常工作,直到该类尚未实现IEnumerable为止,其中T = UserActionLog,复杂类型。

工作正常:

    @Bean
    JwtDecoder jwtDecoder() {
        JWSKeySelector<SecurityContext> jwsKeySelector;
        try {
            URL jwksUrl = new URL(authConfiguration.getJwksUrl());
            JWKSetCache jwkSetCache = new DefaultJWKSetCache(authConfiguration.getJwksCacheTtlMins(),TimeUnit.MINUTES);
            RemoteJWKSet<SecurityContext> jwkSet = new RemoteJWKSet<>(jwksUrl,null,jwkSetCache);
            jwsKeySelector = JWSAlgorithmFamilyJWSKeySelector.fromJWKSource(jwkSet);
        } catch (KeySourceException | MalformedURLException e) {
            throw new InvalidConfigurationException(e.getMessage());
        }

        DefaultJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>();
        jwtProcessor.setJWSKeySelector(jwsKeySelector);
        NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(jwtProcessor);

        OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator(authConfiguration.getAudience());
        OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(authConfiguration.getIssuer());
        OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer,audienceValidator);
        
        jwtDecoder.setJwtValidator(withAudience);

        return jwtDecoder;
    }

但是一旦实现IEnumerable(IEnumerable),反序列化将停止工作。反序列化的值变为空

课程:

public class UserActionLogger
{
    [JsonProperty("userActionLogs")]
    public List<UserActionLog> UserActionLogs { get; set; }
}

发布操作方法:-

public class UserActionLogger : IEnumerable<UserActionLog>
{
    private List<UserActionLog> _userActionLogs;

    [JsonProperty("userActionLogs")]
    public List<UserActionLog> userActionLogs { 
        get
        {
            return _userActionLogs;
        }
        set
        {
            _userActionLogs = value;
        }
    }

    public UserActionLog this[int index]
    {
        get { return _userActionLogs[index]; }
        set { _userActionLogs.Insert(index,value); }
    }

    public IEnumerator<UserActionLog> GetEnumerator()
    {
        return ((IEnumerable<UserActionLog>)_userActionLogs).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

public class UserActionLog
{

    [JsonProperty("eventType")]
    public string EventType { get; set; }

    [JsonProperty("eventSubType")]
    public string EventSubType { get; set; }
   //other properties removed
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)