我有一个带有休息服务的 Spring Web MVC 应用程序,我尝试使用 Springfox 为其提供自动化文档。
我用
io.springfox:springfox-swagger2:2.5.0
io.springfox:springfox-swagger-ui:2.5.0
org.webjars:swagger-ui:2.1.4
我的 Swagger 配置:
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build().apiInfo(apiInfo());
return docket;
}
}
和:
@Configuration
@EnableWebMvc
@Profile("dev")
public class SwaggerWebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
我有一个休息控制器:
@Api
@RestController
@RequestMapping(RegistrationRestController.ROOT_PATH)
public class RegistrationRestController {
private final static Logger log = LogManager.getLogger(RegistrationRestController.class);
public static final String ROOT_PATH = "/rest/registration";
private final ResponseUtil responseUtil;
private final UserService userService;
@Autowired
public RegistrationRestController(ResponseUtil responseUtil, UserService userService) {
log.debug("Instantiate RegistrationRestController bean");
this.responseUtil = Objects.requireNonNull(responseUtil);
this.userService = Objects.requireNonNull(userService);
}
@RequestMapping(value = {""}, method = RequestMethod.POST)
@ApiResponses({
@ApiResponse(code = 200, message = "Create new user and authorize them. Returns access token", response = AccessTokenDTO.class)
})
@ApiOperation(value = "Create new user", response = AccessTokenDTO.class)
public ResponseDTO newUserRegistration(@Valid @ApiParam(required = true) RegistrationDTO registrationDTO) {
log.debug("POST {} with registrationDTO='{}'", ROOT_PATH, registrationDTO.toString());
AccessTokenDTO accessToken = userService.createUserAndAuthorize(registrationDTO);
return responseUtil.wrapResult(accessToken);
}
}
和注册DTO:
@ApiModel
public class RegistrationDTO {
@NotNull(message = "registrationDTO.phone.notNull.fail")
@Pattern(regexp = "[0-9]{10}", message = "registrationDTO.phone.pattern.fail")
private String phone;
@NotNull(message = "registrationDTO.email.notNull.fail")
@Email(message = "registrationDTO.email.pattern.fail")
private String email;
@NotNull(message = "registrationDTO.lastName.notNull.fail")
@Size(min = 1, message = "registrationDTO.lastName.size.fail")
private String lastName;
@NotNull(message = "registrationDTO.firstName.notNull.fail")
@Size(min = 1, message = "registrationDTO.firstName.size.fail")
private String firstName;
@NotNull(message = "registrationDTO.password.notNull.fail")
@Size(min = 6, message = "registrationDTO.password.size.fail")
private String password;
@NotNull(message = "registrationDTO.passwordConfirmation.notNull.fail")
@Size(min = 6, message = "registrationDTO.passwordConfirmation.size.fail")
private String passwordConfirmation;
@AssertTrue(message = "registrationDTO.isPasswordMatch.fail")
public boolean isPasswordMatch() {
return Objects.equals(password, passwordConfirmation);
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirmation() {
return passwordConfirmation;
}
public void setPasswordConfirmation(String passwordConfirmation) {
this.passwordConfirmation = passwordConfirmation;
}
@Override
public String toString() {
return "RegistrationDTO{" +
"phone='" + phone + '\'' +
", email='" + email + '\'' +
", lastName='" + lastName + '\'' +
", firstName='" + firstName + '\'' +
'}';
}
}
但是当我打开 localhost:8080/swagger-ui.html 时,我看到 RegistrationDTO 参数的数据类型为空。
我做错了什么?:(