开发工具 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)
解决:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.water</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>swagger</name>
<description>Demo project for swagger</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.battcn</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>2.1.5-RELEASE</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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