好久没上了, 看到又有人回复了. 我就来修改一下. 修改时间  2018年5月16日

这回给你上全新版本. 至发稿时间,所有的包都是新版.

注意: 高版本需要添加  jaxb-api 包, 否则会报错. 最下面列出报错信息

2018年5月17日更新  jdk8 不需要添加  jaxb-api

版本:

jdk 10

spring boot 2.0.2

swagger 2.8.0

  1. pom.xml 包引入
 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>swagger</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>swagger</name>
  10. <description>Demo project for Spring Boot</description>
  11. <!-- 华为Maven镜像 当然你也可以用阿里的-->
  12. <repositories>
  13. <repository>
  14. <id>huaweicloud</id>
  15. <url>https://repo.huaweicloud.com/repository/maven/</url>
  16. <!--<url>http://maven.aliyun.com/nexus/content/groups/public/</url>-->
  17. <releases>
  18. <enabled>true</enabled>
  19. </releases>
  20. <snapshots>
  21. <enabled>true</enabled>
  22. <updatePolicy>always</updatePolicy>
  23. <checksumPolicy>fail</checksumPolicy>
  24. </snapshots>
  25. </repository>
  26. </repositories>
  27. <parent>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-parent</artifactId>
  30. <version>2.0.2.RELEASE</version>
  31. <relativePath/> <!-- lookup parent from repository -->
  32. </parent>
  33. <properties>
  34. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  35. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  36. <java.version>10</java.version>
  37. </properties>
  38. <dependencies>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-web</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>io.springfox</groupId>
  50. <artifactId>springfox-swagger2</artifactId>
  51. <version>2.8.0</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>io.springfox</groupId>
  55. <artifactId>springfox-swagger-ui</artifactId>
  56. <version>2.8.0</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>javax.xml.bind</groupId>
  60. <artifactId>jaxb-api</artifactId>
  61. <version>2.3.0</version>
  62. </dependency>
  63. </dependencies>
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. </plugin>
  70. </plugins>
  71. </build>
  72. </project>

先检查Maven中包是否导入, 以下以idea为列. 看是否有 swagger-ui 这个包

以下为代码配置:


  1. package net.ainio.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  11. @Configuration
  12. @EnableSwagger2
  13. public class SwaggerConfig {
  14. @Bean
  15. public Docket api() {
  16. return new Docket(DocumentationType.SWAGGER_2)
  17. .apiInfo(apiInfo())
  18. .select()
  19. // 自行修改为自己的包路径
  20. .apis(RequestHandlerSelectors.basePackage("net.ainio.controller"))
  21. .paths(PathSelectors.any())
  22. .build();
  23. }
  24. private ApiInfo apiInfo() {
  25. return new ApiInfoBuilder()
  26. .title("api文档")
  27. .description("restfun 风格接口")
  28. //服务条款网址
  29. //.termsOfServiceUrl("http://blog.csdn.net/forezp")
  30. .version("1.0")
  31. //.contact(new Contact("帅呆了", "url", "email"))
  32. .build();
  33. }
  34. }

测试controller

注意下这两个的区别哦, 不然前台会返回 404 错误

@RestController
@Controller

  1. package net.ainio.controller;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiOperation;
  5. import org.apache.catalina.User;
  6. import org.springframework.web.bind.annotation.*;
  7. @Api(value="/test", tags="测试接口模块")
  8. @RestController
  9. @RequestMapping("/test")
  10. public class TestSwaggerController {
  11. @ApiOperation(value="展示首页信息", notes = "展示首页信息")
  12. @GetMapping("/show")
  13. public Object showInfo(){
  14. return "hello world";
  15. }
  16. @ApiOperation(value="添加用户信息", notes = "添加用户信息")
  17. @ApiImplicitParam(name="user", value="User", required = true, dataType = "User")
  18. @PostMapping("/addUser")
  19. public Object addUser(@RequestBody User user){
  20. return "success";
  21. }
  22. }

启动后访问

http://localhost:8080/swagger-ui.html

感谢 Michael_Zhan_Tcys 网友

下面列出, 不添加 jabx-api的报错信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2018-05-16 23:16:20.452 ERROR 10220 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

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:262) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1202) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]

at com.example.SwaggerApplication.main(SwaggerApplication.java:10) [classes/:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.2.RELEASE.jar:2.0.2.RELEASE]

Caused by: java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.XmlModelPlugin] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@4629104a]

at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:245) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]

... 23 common frames omitted

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlType

at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]

at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3119) ~[na:na]

at java.base/java.lang.Class.getDeclaredMethods(Class.java:2268) ~[na:na]

at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]

... 26 common frames omitted

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlType

at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) ~[na:na]

at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190) ~[na:na]

at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499) ~[na:na]

... 30 common frames omitted

Process finished with exit code 0

本文链接:https://blog.csdn.net/aPiFen/article/details/79397100

2018 最新 spring boot 整合 swagger2 (swagger2 版本 2.8.0)的更多相关文章

  1. spring boot 整合mybatis + swagger2

    之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到了spring boot 后发现搭建一个web项目真的是1分 ...

  2. Spring Boot整合JPA、Redis和Swagger2

    好久没有总结了,最近也一直在学习.今天就把spring boot与其它技术的整合做个小总结,主要是jpa.redis和swagger2.公司里有用到这些,整合起来也很简单. 首先,新建一个Spring ...

  3. spring boot整合Swagger2

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服务器 ...

  4. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  5. spring boot 整合 swagger2

    swagger2为了更好的管理api文档接口 swagger构建的api文档如下,清晰,避免了手写api诸多痛点 一,添加依赖 <!--swagger2的官方依赖--> <depen ...

  6. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  7. Spring Boot中使用Swagger2自动构建API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  8. Spring Boot中使用Swagger2构建RESTful API文档

    在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...

  9. Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

    项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...

随机推荐

  1. SpringMVC入门及拦截器

    SSM最后一个框架springmvc,其实上手特别简单.但是我昨天看一个深入源码的视频,差点GG.其实以前学过很多东西,都忘了,不敢说学会,现在有了本书,看过一遍之后.多多少少记住一些,权当我会用了, ...

  2. python#父与子编程之旅-11嵌套与可变循环

    for multiplier in range(5,8): for i in range(2,11): print(i,"x",multiplier,"=",i ...

  3. python-模块 time, os, sys

    时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...

  4. 38-python基础-python3-检查字典中是否存在键或值

    in 和 not in 操作符   请注意, 在前面的例子中,‘name’ in spam 本质上是一个简写版本.相当于'name' in spam.keys()

  5. 微服务-熔断器 Hystrix 的原理与使用

    前言 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为了应对服务雪崩, 一种常见的做法是手动服务降级. 而Hystrix的出现,给我们提供了另一种选 ...

  6. c# DataTable join 两表连接

    转:https://www.cnblogs.com/xuxiaona/p/4000344.html JlrInfodt和dtsource是两个datatable,通过[姓名]和[lqry]进行关联 v ...

  7. 【记录】gitLab git命令add commit fetch pull push

    最近项目使用git进行版本控制,由于之前用svn,所以对git不是太熟悉,网上一通乱找git各命令含义, 以下内容感觉讲的很详细,可以很清楚理解git提交流程,博主把重要的信息用红字标注了,更加显眼. ...

  8. Codeforces 1150D DP

    题意:给你一个长度为n的字符串,有q次询问,每次询问会给字符串x的末尾添加一个字符y,或者删除字符串x末尾的字符,询问过后,要判断长度为n的字符串中是否有3个不重合的子序列,是这3个字符串. 思路:设 ...

  9. 笔记66 Spring Boot快速入门(六)

    SpringBoot中使用Mybatis 一.注解方式 1.创建映射文件CategoryMapper.java 使用注解@Mapper 表示这是一个Mybatis Mapper接口.使用@Select ...

  10. HTTP/2的优先级

    前言 记得HTTP/3即将标准化了.今日早读文章由@smallbonelu翻译授权分享. @smallbonelu,一枚爱好跑步的前端工程师 正文从这开始-- 以正确的顺序请求页面资源对于快速的用户体 ...