org.springframework.core.convert.converter.Converter.convert()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(234)

本文整理了Java中org.springframework.core.convert.converter.Converter.convert()方法的一些代码示例,展示了Converter.convert()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Converter.convert()方法的具体详情如下:
包路径:org.springframework.core.convert.converter.Converter
类名称:Converter
方法名:convert

Converter.convert介绍

[英]Convert the source object of type S to target type T.
[中]将S类型的源对象转换为T类型的目标对象。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public int compare(S o1, S o2) {
  T c1 = this.converter.convert(o1);
  T c2 = this.converter.convert(o2);
  return this.comparator.compare(c1, c2);
}

代码示例来源:origin: org.springframework/spring-core

@Override
public int compare(S o1, S o2) {
  T c1 = this.converter.convert(o1);
  T c2 = this.converter.convert(o2);
  return this.comparator.compare(c1, c2);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return convertNullSource(sourceType, targetType);
  }
  return this.converter.convert(source);
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Extracts the {@link GrantedAuthority}s from scope attributes typically found in a {@link Jwt}
 *
 * @param jwt The token
 * @return The collection of {@link GrantedAuthority}s found on the token
 * @deprecated Since 5.2. Use your own custom converter instead
 * @see JwtGrantedAuthoritiesConverter
 * @see #setJwtGrantedAuthoritiesConverter(Converter)
 */
@Deprecated
protected Collection<GrantedAuthority> extractAuthorities(Jwt jwt) {
  return this.jwtGrantedAuthoritiesConverter.convert(jwt);
}

代码示例来源:origin: org.springframework/spring-core

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return convertNullSource(sourceType, targetType);
  }
  return this.converter.convert(source);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return convertNullSource(sourceType, targetType);
  }
  return this.converterFactory.getConverter(targetType.getObjectType()).convert(source);
}

代码示例来源:origin: spring-projects/spring-security

@Override
  public Flux<GrantedAuthority> convert(Jwt jwt) {
    return Flux.fromIterable(this.grantedAuthoritiesConverter.convert(jwt));
  }
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<AbstractAuthenticationToken> convert(Jwt jwt) {
  return this.jwtGrantedAuthoritiesConverter.convert(jwt)
      .collectList()
      .map(authorities -> new JwtAuthenticationToken(jwt, authorities));
}

代码示例来源:origin: org.springframework/spring-core

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return convertNullSource(sourceType, targetType);
  }
  return this.converterFactory.getConverter(targetType.getObjectType()).convert(source);
}

代码示例来源:origin: spring-projects/spring-security

@Override
protected void writeInternal(OAuth2Error oauth2Error, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {
  try {
    Map<String, String> errorParameters = this.errorParametersConverter.convert(oauth2Error);
    this.jsonMessageConverter.write(
        errorParameters, PARAMETERIZED_RESPONSE_TYPE.getType(), MediaType.APPLICATION_JSON, outputMessage);
  } catch (Exception ex) {
    throw new HttpMessageNotWritableException("An error occurred writing the OAuth 2.0 Error: " + ex.getMessage(), ex);
  }
}

代码示例来源:origin: spring-projects/spring-security

@Override
protected void writeInternal(OAuth2AccessTokenResponse tokenResponse, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {
  try {
    Map<String, String> tokenResponseParameters = this.tokenResponseParametersConverter.convert(tokenResponse);
    this.jsonMessageConverter.write(
        tokenResponseParameters, PARAMETERIZED_RESPONSE_TYPE.getType(), MediaType.APPLICATION_JSON, outputMessage);
  } catch (Exception ex) {
    throw new HttpMessageNotWritableException("An error occurred writing the OAuth 2.0 Access Token Response: " + ex.getMessage(), ex);
  }
}

代码示例来源:origin: spring-projects/spring-security

@Override
protected OAuth2AccessTokenResponse readInternal(Class<? extends OAuth2AccessTokenResponse> clazz, HttpInputMessage inputMessage)
    throws IOException, HttpMessageNotReadableException {
  try {
    @SuppressWarnings("unchecked")
    Map<String, String> tokenResponseParameters = (Map<String, String>) this.jsonMessageConverter.read(
        PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage);
    return this.tokenResponseConverter.convert(tokenResponseParameters);
  } catch (Exception ex) {
    throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Access Token Response: " +
        ex.getMessage(), ex, inputMessage);
  }
}

代码示例来源:origin: spring-projects/spring-security

private Jwt createJwt(JWT parsedJwt, JWTClaimsSet jwtClaimsSet) {
  Map<String, Object> headers = new LinkedHashMap<>(parsedJwt.getHeader().toJSONObject());
  Map<String, Object> claims = this.claimSetConverter.convert(jwtClaimsSet.getClaims());
  Instant expiresAt = (Instant) claims.get(JwtClaimNames.EXP);
  Instant issuedAt = (Instant) claims.get(JwtClaimNames.IAT);
  return new Jwt(parsedJwt.getParsedString(), issuedAt, expiresAt, headers, claims);
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void decodeWhenUsingSignedJwtThenReturnsClaimsGivenByClaimSetConverter() {
  Converter<Map<String, Object>, Map<String, Object>> claimSetConverter = mock(Converter.class);
  this.decoder.setClaimSetConverter(claimSetConverter);
  when(claimSetConverter.convert(any(Map.class))).thenReturn(Collections.singletonMap("custom", "value"));
  Jwt jwt = this.decoder.decode(this.messageReadToken).block();
  assertThat(jwt.getClaims().size()).isEqualTo(1);
  assertThat(jwt.getClaims().get("custom")).isEqualTo("value");
  verify(claimSetConverter).convert(any(Map.class));
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void convertWhenConverterReturnsValueWhenEntryIsMissingThenEntryIsAdded() {
  Converter<Object, String> claimConverter = mock(Converter.class);
  MappedJwtClaimSetConverter converter = MappedJwtClaimSetConverter
      .withDefaults(Collections.singletonMap("custom-claim", claimConverter));
  when(claimConverter.convert(any())).thenReturn("custom-value");
  Map<String, Object> source = new HashMap<>();
  Map<String, Object> target = converter.convert(source);
  assertThat(target.get("custom-claim")).isEqualTo("custom-value");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void convertWhenUsingCustomExpiresAtConverterThenIssuedAtConverterStillConsultsIt() {
  Instant at = Instant.ofEpochMilli(1000000000000L);
  Converter<Object, Instant> expiresAtConverter = mock(Converter.class);
  when(expiresAtConverter.convert(any())).thenReturn(at);
  MappedJwtClaimSetConverter converter = MappedJwtClaimSetConverter
      .withDefaults(Collections.singletonMap(JwtClaimNames.EXP, expiresAtConverter));
  Map<String, Object> source = new HashMap<>();
  Map<String, Object> target = converter.convert(source);
  assertThat(target.get(JwtClaimNames.IAT)).
      isEqualTo(Instant.ofEpochMilli(at.toEpochMilli()).minusSeconds(1));
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void decodeWhenUsingSignedJwtThenReturnsClaimsGivenByClaimSetConverter() {
  Converter<Map<String, Object>, Map<String, Object>> claimSetConverter = mock(Converter.class);
  when(claimSetConverter.convert(any(Map.class)))
      .thenReturn(Collections.singletonMap("custom", "value"));
  this.jwtDecoder.setClaimSetConverter(claimSetConverter);
  Jwt jwt = this.jwtDecoder.decode(SIGNED_JWT);
  assertThat(jwt.getClaims().size()).isEqualTo(1);
  assertThat(jwt.getClaims().get("custom")).isEqualTo("value");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
  Converter errorConverter = mock(Converter.class);
  when(errorConverter.convert(any())).thenThrow(RuntimeException.class);
  this.messageConverter.setErrorConverter(errorConverter);
  String errorResponse = "{}";
  MockClientHttpResponse response = new MockClientHttpResponse(
      errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
  assertThatThrownBy(() -> this.messageConverter.readInternal(OAuth2Error.class, response))
      .isInstanceOf(HttpMessageNotReadableException.class)
      .hasMessageContaining("An error occurred reading the OAuth 2.0 Error");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
  Converter tokenResponseConverter = mock(Converter.class);
  when(tokenResponseConverter.convert(any())).thenThrow(RuntimeException.class);
  this.messageConverter.setTokenResponseConverter(tokenResponseConverter);
  String tokenResponse = "{}";
  MockClientHttpResponse response = new MockClientHttpResponse(
      tokenResponse.getBytes(), HttpStatus.OK);
  assertThatThrownBy(() -> this.messageConverter.readInternal(OAuth2AccessTokenResponse.class, response))
      .isInstanceOf(HttpMessageNotReadableException.class)
      .hasMessageContaining("An error occurred reading the OAuth 2.0 Access Token Response");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt() {
  BearerTokenAuthenticationToken token = this.authentication();
  Map<String, Object> claims = new HashMap<>();
  claims.put("name", "value");
  Jwt jwt = this.jwt(claims);
  when(this.jwtDecoder.decode("token")).thenReturn(jwt);
  when(this.jwtAuthenticationConverter.convert(jwt)).thenReturn(new JwtAuthenticationToken(jwt));
  JwtAuthenticationToken authentication =
      (JwtAuthenticationToken) this.provider.authenticate(token);
  assertThat(authentication.getTokenAttributes()).isEqualTo(claims);
}

相关文章

微信公众号

最新文章

更多

Converter类方法