开发工具 idea 2018.3.4
运行出错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlModelPlugin': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.XmlModelPlugin] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@4629104a] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:265)
解决:
javax.xml.bind
jaxb-api
2.3.0
代码:
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.water
swagger
0.0.1-SNAPSHOT
swagger
Demo project for swagger
1.8
org.springframework.boot
spring-boot-starter-web
com.battcn
swagger-spring-boot-starter
2.1.5-RELEASE
javax.xml.bind
jaxb-api
2.3.0
org.springframework.boot
spring-boot-starter-log4j
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
SwaggerApplication.java
package com.water.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
User.java
package com.water.swagger;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel
public class User implements Serializable {
private static final long serialVersionUID = 8655851615465363473L;
@ApiModelProperty("用户ID")
private Long id;
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
public User() {
}
public User(Long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
String str = "{User: "+
"id=" + id +
", username=" + username +
", password=" + password +
" }";
return str;
}
}
UserController.java
package com.water.swagger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
/*
* http://localhost:8080/swagger-ui.html
* */
@Api(tags = "0.1", description = "用户管理", value = "用户管理v")
public class UserController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);
/*
* GET http://localhost:8080/users?username="lucy"&password="123456"
* */
@ApiOperation(value = "条件查询")
@ApiImplicitParams({
@ApiImplicitParam(name="username", value = "用户名", dataType ="1" , paramType = "11"),
@ApiImplicitParam(name="password", value = "用户密码")
})
@GetMapping
public User query(String username, String password) {
System.out.println("username="+username +", password=" + password);
return new User(1L, username, password);
}
/*
* GET http://localhost:8080/users/1
* */
@ApiOperation(value = "主键查询")
@ApiImplicitParam(name="id", value = "用户编号")
@GetMapping("/{id}")
public User get(@PathVariable Long id) {
log.debug("id=" +id);
return new User(id, "u"+id, "p"+id);
}
/*
* DELETE http://localhost:8080/users/1
* */
@ApiOperation(value = "删除用户")
@ApiImplicitParam(name="id", value = "用户编号")
@DeleteMapping("/{id}")
public String delete(@PathVariable Long id){
System.out.println("delete id="+id);
return "操作成功,id:" + id;
}
/*
* POST http://localhost:8080/users/
*
* {
"id":1,
"username":"peter",
"password":"123456"
}
*
* */
@ApiOperation(value = "添加用户")
@PostMapping
public User post(@RequestBody User user){
System.out.println(user.toString());
return user;
}
/*
* PUT http://localhost:8080/users/1
*
* {
"id":1,
"username":"peter",
"password":"123456"
}
*
* */
@ApiOperation(value = "修改用户")
@ApiImplicitParam(name="id", value="用户ID")
@PutMapping("/{id}")
public String put(@PathVariable Long id, @RequestBody User user){
System.out.println("id=" + id + ", user=" + user.toString());
return "操作成功,id:" + id;
}
}
浏览链接:http://localhost:8080/swagger-ui.html
参考:
https://blog.csdn.net/Winter_chen001/article/details/80748253