【发布时间】:2020-09-23 15:29:31
【问题描述】:
我成功地将 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, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in …
不确定它与大摇大摆的集成有什么关系。如果我删除 swagger 集成代码,它可以使用相同的代码正常工作,并且不会抱怨类型转换失败。
为了解决这个问题,我也采纳了某人的建议
升级 com.fasterxml.jackson.core 的依赖项
并重建代码。但还是没有成功。
compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.11.2'
我尝试过但没有解决的问题:
- 添加了默认/空构造函数 (对于大多数其他有类似问题的人来说,它的工作原理是这样的,对我来说,它会在之后抱怨
error: variable id might not have been initialized
}
- 将此添加到 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)...
【问题讨论】:
-
你添加了什么代码?
-
看起来它无法将代码中的某些内容转换为 swagger 可以理解/显示的内容,但是没有看到您的代码,很难说出是什么/为什么,正如@Helen 指出的那样。
标签: spring-security jackson swagger token jackson-databind