集成的Swagger和以前工作的代码中断:无法从Object值反序列化没有基于委托或基于属性的Creator

问题描述

我成功地将swagger集成到了多个spring boot服务中。
必须通过添加扩展WebSecurityConfigurerAdapter的@EnableWebSecurity类来允许端点绕过身份验证(这对于其他服务也适用):

 
 @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(1)
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

...

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .antMatcher("/**")
       
       ...
                .antMatchers("/actuator/**").permitAll()
                .antMatchers("/v2/api-docs","/configuration/**","/webjars/**","/swagger*/**") // ADDED THIS for swagger 
                .permitAll()  // ADDED THIS for swagger 
                .antMatchers("/challenge").permitAll()
                .antMatchers("/token").permitAll()  // ENDPOINT with complaint Now,that was prevIoUsly ok.
               .anyRequest()
                .authenticated()
                .and()
                .cors();
    }
    ...
    }

但是,对于特定的代码,一旦我添加了相关的摇摇欲坠的代码和依赖项,它似乎就坏了,并抱怨最初起什么作用。

这是投诉的终点:

@PostMapping("/token")
    public ResponseDto token(@Valid @RequestBody TokenRequest request) {
        try {
            return service.generateJwtFromCode(request.getId(),request.getCode());
        }
        ...
        catch (Exception exception) {..
        }
        }

没有找到此类的构造方法上的嵌套异常:

@AllArgsConstructor
public class TokenRequest {

    @NotEmpty
    @JsonProperty
    private final String id;

    @NotEmpty
    @Getter
    private final String code;

    public UUID getId() {
        return UUID.fromString(id);
    }
  

}
Could not resolve parameter [0] in responseDTO  Controller.token(Service.TokenRequest): Type deFinition error: [simple type,class TokenRequest]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDeFinitionException: Cannot construct instance of `service.TokenRequest` (no Creators,like default construct,exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (pushbackinputstream); line: 1,column: 2]
 o.s.web.servlet.dispatcherServlet        : Failed to complete request: org.springframework.http.converter.HttpMessageConversionException: Type deFinition error: [simple type,class service.TokenRequest]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDeFinitionException: Cannot construct instance of `service.TokenRequest` (no Creators,column: 2]
o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in …

不确定它与招摇整合有什么关系。如果我删除了摇摇欲坠的集成代码,它可以与相同的代码一起正常工作,并且不会抱怨类型转换失败。

为了解决这个问题,我也接受了某人的建议
升级com.fasterxml.jackson.core的依赖项 并重建代码。但是仍然没有成功。

  compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.11.2'

我尝试过但无法解决的事情

  1. 添加认/空构造函数 (对于大多数有类似问题的其他人,它都是这样做的,对我来说,此后我就投诉了
error: variable id might not have been initialized
    }
  1. 将此添加到tokenRequest类:
@Value
@AllArgsConstructor(onConstructor = @__(@JsonCreator(mode = JsonCreator.Mode.PROPERTIES))

出现其他错误

c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type,class TokenRequest]]
InvalidDeFinitionException: Invalid type deFinition for type `TokenRequest`: More than one argument (#0 and #1) left as delegating for Creator [constructor for TokenRequest,annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DELEGATING)}]: only one allowed
    at com.fasterxml.jackson.databind.exc.InvalidDeFinitionException.from(InvalidDeFinitionException.java:62)...

解决方法

解决方案是添加默认构造函数,并删除最终变量。

@AllArgsConstructor
public class TokenRequest {

    @NotEmpty
    @JsonProperty
    private String id;  // code fixed issue

    @NotEmpty
    @Getter
    private String code;  // code fixed issue

    public TokenRequest(){}  // code fixed issue
    
    public UUID getId() {
        return UUID.fromString(id);
    }
  


}