我正在Android(kotlin)项目中使用Retrofit。
我创建了我的界面:
interface StudentsInterface {
@GET("foo/bar/{id}")
suspend fun getStudent(@Path("id") myId: Int)
}
我创建了一个MyClient
类,在其中定义了一个通用函数,用于从上述定义的任何接口创建端点服务:
class MyClient() {
@Inject
lateinit var retrofit: Retrofit
// this is my generic function
fun <T> createService(interfaceClazz: Class<T>?): T {
// Compiler error: Type mismatch: inferred type is Class<T>? but Class<T!> was expected
return retrofit.create(interfaceClazz)
}
}
这样我可以在另一堂课中
val sService = myClient.createService(StudentsInterface::class.java)
...
但是当我构建项目时,总是会遇到编译器错误:在代码Type mismatch: inferred type is Class<T>? but Class<T!> was expected
行中的return retrofit.create(interfaceClazz)
为什么会出现此错误?如何摆脱它?
答案 0 :(得分:1)
改造创建需要不可为空的参数。尝试使@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${auth.issuer-uri}")
String authIssuerUri;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()//enable later
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and()
.authorizeRequests(
ar -> ar
.antMatchers("/register").permitAll()
.anyRequest().authenticated())
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
// this should be used on authorization
@Bean
JwtDecoder jwtDecoder() {
//tried to replicate what spring does by default by adding one more JwtAudienceValidator
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
validators.add(new JwtIssuerValidator(authIssuerUri));
validators.add(new JwtAudienceValidator());
NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromIssuerLocation(authIssuerUri);
jwtDecoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(validators));
return jwtDecoder;
}
}
不可为空
auth.issuer-uri= from system props
spring.security.oauth2.resourceserver.jwt.issuer-uri=${auth.issuer-uri}
#spring.security.oauth2.client.provider.okta.issuer-uri= ${auth.issuer-uri} tried this also adding relevant spring gradle deps