使用io.springfox配置swagger

cxf(2.7.18) spring(3.2.7.RELEASE) struts2(2.3.36).如果使用高于3.2.7.RELEASE的,在struts序列化json时会提示TruePointcut无法序列化的错误

一、使用io.springfox(2.0.3)。

参考文档:http://springfox.github.io/springfox/docs/current/#configuring-springfox-staticdocs。

https://stackoverflow.com/questions/26720090/a-simple-way-to-implement-swagger-in-a-spring-mvc-application

跟spring mvc集成,建议使用spring3.2及以上。自动扫描识别@RequestMapping标注的类和接口,生成接口文档。没有使用RequestMapping标注的将不会生成。 

根据@Api生成接口分组及描述。

根据@RequestMapping生成接口,根据@ApiOperation生产接口描述

二、配置

增加配置类

@EnableWebMvc
@EnableSwagger2
public class ApplicationSwaggerConfig {
	@Bean
    public Docket customTest1Docket() {
        return new Docket(DocumentationType.SWAGGER_12).groupName("test1")
                .apiInfo(apiInfo());
    }
	
	@Bean
	 public Docket customTest2Docket() {
	        return new Docket(DocumentationType.SWAGGER_2).groupName("test2")
	                .apiInfo(apiInfo());
	    }
	 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("REST API 接口")
                .description("REST API接口")
                .contact("www.wsncm.com")
                .version("1.1.0")
                .build();
    }
}

pom.xml增加

		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-swagger2</artifactId>
		    <version>2.0.3</version>
		</dependency>
		
		<dependency>
		    <groupId>io.springfox</groupId>
		    <artifactId>springfox-swagger-ui</artifactId>
		    <version>2.0.3</version>
		</dependency>

web.xml增加

	<servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContextmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/configuration/security</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/configuration/ui</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/swagger-resources</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/v2/api-docs</url-pattern>
	</servlet-mapping>

增加web配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
			http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
	<context:component-scan base-package="com.wsn.swagger,com.wsn.deviceIO.lib.rest,springfox.*"/>
    <!-- Enables swgger ui-->
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    <!-- Include a swagger configuration-->
    <bean name="applicationSwaggerConfig" class="com.wsn.swagger.ApplicationSwaggerConfig"/>
</beans>

编写对应的接口并注解.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用中的内容,解决swagger2.9.2报错 For input string: "" 的问题时,可以尝试将swagger的annotations以及models替换为1.5.21版本。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.22</version> </dependency> ``` 这样,你就能解决swagger2.9.2报错 For input string: "" 的问题了。如果你还遇到swagger不能传递参数的问题,可以参考引用中提供的博客链接,了解解决方案。 在使用swagger传递参数时,你可以使用@ApiParam注解,如引用所示。在方法的参数上添加@ApiParam注解,指定参数的名称、值和是否必需。这样,你就能够在swagger中正确传递参数了。但如果你仍然遇到Cannot resolve io.springfox:springfox-swagger-ui:unknown的问题,可能是你的项目中没有正确引入swagger-ui的依赖。你可以检查一下你的pom.xml文件,确保有以下依赖的配置: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 这样,你就能够解决Cannot resolve io.springfox:springfox-swagger-ui:unknown的问题了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决idea中添加依赖遇见Cannot resolve XXX的问题](https://blog.csdn.net/qq_56392291/article/details/131025683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [swagger使用问题收集](https://blog.csdn.net/DATANGguanjunhou/article/details/102733213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值