如何在 Spring 引导内置的 swagger 页面中添加 textbox/textfield?
How can I add textbox/textfield in my swagger page built in Spring boot?
我正在尝试使用注释在 Spring 引导中为 RestAPI 自动生成 swagger 页面。
控制器代码:
@RestController
@Api(value="UserManagementAPI", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserManagementController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@ApiOperation(value="add a pro",consumes="application/json")
@RequestMapping(value = "/getUser", method = RequestMethod.GET, produces="application/json")
public static List<UserDetails> getUser(@PathVariable(name="id") String id) throws UserException
{
return UserHelper.getUserByEmail(id);
}
Application.java
@SpringBootApplication
@EnableSwagger2
@Configuration
@ComponentScan({ "userManagement"})
@EnableAutoConfiguration
public class Application {
@Bean
public Docket simpleDiffServiceApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("userManagement").apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any())
// .paths(PathSelectors.any())
// Will also include the basic error controllers by default
.paths(Predicates.not(PathSelectors.regex("/error")))
// Exclude basic error controllers
.build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Business Location Service")
.description("Spring Boot REST APIs for Business Location Service")
.contact(new Contact("EntSol-IoT (Niche Technology Delivery Group)", "http://somewebsite.com",
"some@mail.com"))
.version("1.0").build();
}
在 swagger 页面中,我可以看到我所有的 API。但还有更多。它显示了所有可能的方法类型(例如 POST、GET、PUT 等),尽管在 Controller 中我只写了 GET 方法。
另一个问题是 API 下的 swagger 页面中没有文本框,我可以在其中搜索 id。可能是我遗漏了什么。过去两天我一直在努力解决它。却忍不住自己。提前致谢。
我明白了。您的 getUser
方法声明为 static
。请删除 static
,以使其正常工作。
public List<UserDetails> getUser(@PathVariable(name="id") String id) throws UserException { }
我正在尝试使用注释在 Spring 引导中为 RestAPI 自动生成 swagger 页面。
控制器代码:
@RestController
@Api(value="UserManagementAPI", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserManagementController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@ApiOperation(value="add a pro",consumes="application/json")
@RequestMapping(value = "/getUser", method = RequestMethod.GET, produces="application/json")
public static List<UserDetails> getUser(@PathVariable(name="id") String id) throws UserException
{
return UserHelper.getUserByEmail(id);
}
Application.java
@SpringBootApplication
@EnableSwagger2
@Configuration
@ComponentScan({ "userManagement"})
@EnableAutoConfiguration
public class Application {
@Bean
public Docket simpleDiffServiceApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("userManagement").apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any())
// .paths(PathSelectors.any())
// Will also include the basic error controllers by default
.paths(Predicates.not(PathSelectors.regex("/error")))
// Exclude basic error controllers
.build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Business Location Service")
.description("Spring Boot REST APIs for Business Location Service")
.contact(new Contact("EntSol-IoT (Niche Technology Delivery Group)", "http://somewebsite.com",
"some@mail.com"))
.version("1.0").build();
}
在 swagger 页面中,我可以看到我所有的 API。但还有更多。它显示了所有可能的方法类型(例如 POST、GET、PUT 等),尽管在 Controller 中我只写了 GET 方法。 另一个问题是 API 下的 swagger 页面中没有文本框,我可以在其中搜索 id。可能是我遗漏了什么。过去两天我一直在努力解决它。却忍不住自己。提前致谢。
我明白了。您的 getUser
方法声明为 static
。请删除 static
,以使其正常工作。
public List<UserDetails> getUser(@PathVariable(name="id") String id) throws UserException { }