SpringBoot解析YAML到JAVAbean异常elements left unbound

情况:YAML到Java bean的转化

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "")
@Data
public class AllowedCrossroads {

  private List<CrossroadCombination> allowed;
}

@Data
@Builder
@AllArgsConstructor
public class CrossroadCombination {

  private String firstCrossroad;
  private String secondCrossroad;
  private String thirdCrossroad;
  private String fourthCrossroad;
}

异常:elements left unbound.

解决方式:

内部类变为静态的、加上@NoArgsConstructor注解

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class CrossroadCombination {

  private String firstCrossroad;
  private String secondCrossroad;
  private String thirdCrossroad;
  private String fourthCrossroad;

参考:java - Reason: The elements were left unbound - Stack Overflow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot中启用跨域资源共享(CORS),您可以使用Spring框架提供的CorsFilter类或使用自定义过滤器。 在application.yaml文件中添加以下配置来启用CorsFilter: ```yaml spring: cors: allowed-origins: "*" allowed-methods: "GET, POST, PUT, DELETE, OPTIONS" allowed-headers: "Authorization, Content-Type" allow-credentials: true max-age: 3600 ``` - allowed-origins: 允许的来源,"*"表示所有来源。 - allowed-methods: 允许的请求方法。 - allowed-headers: 允许的请求头。 - allow-credentials: 是否允许发送cookie。 - max-age: 预检请求的有效期。 如果您想使用自定义过滤器,可以在配置类中创建一个@Bean方法来创建该过滤器并将其添加到过滤器链中: ```java @Configuration public class WebConfig { @Bean public FilterRegistrationBean corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowedOrigins(Arrays.asList("*")); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); config.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type")); config.setAllowCredentials(true); config.setMaxAge(3600L); source.registerCorsConfiguration("/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); return bean; } } ``` 这将创建一个名为corsFilter的过滤器,并将其添加到过滤器链中,以便在处理请求之前进行CORS检查。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值