标签:res client encoding commons sch 整合 model aging 资源
对Swagger写个独立的工程测试,方便后续进行工程的整合,这里做一下记录
1.pom.xml--依赖的的包
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com</groupId> 4 <artifactId>com.swagger</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 <properties> 9 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 10 资源构建编码 --> 11 <spring.version>4.1.3.RELEASE</spring.version> 12 <springfox.version>2.4.0</springfox.version> 13 <jackson.version>2.7.3</jackson.version> 14 </properties> 15 16 <dependencies> 17 18 <!-- springfox依赖 --> 19 <dependency> 20 <groupId>io.springfox</groupId> 21 <artifactId>springfox-swagger2</artifactId> 22 <version>${springfox.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>io.springfox</groupId> 26 <artifactId>springfox-swagger-ui</artifactId> 27 <version>${springfox.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>com.google.guava</groupId> 31 <artifactId>guava</artifactId> 32 <version>15.0</version> 33 </dependency> 34 <dependency> 35 <groupId>com.fasterxml.jackson.core</groupId> 36 <artifactId>jackson-databind</artifactId> 37 <version>${jackson.version}</version> 38 </dependency> 39 40 <!-- 测试框架 --> 41 <dependency> 42 <groupId>junit</groupId> 43 <artifactId>junit</artifactId> 44 <version>3.8.1</version> 45 <scope>test</scope> 46 </dependency> 47 48 <!-- Spring start --> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-orm</artifactId> 53 <version>${spring.version}</version> 54 <exclusions> 55 <exclusion> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-core</artifactId> 58 </exclusion> 59 </exclusions> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-tx</artifactId> 64 <version>${spring.version}</version> 65 <exclusions> 66 <exclusion> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-core</artifactId> 69 </exclusion> 70 </exclusions> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-context</artifactId> 75 <version>${spring.version}</version> 76 <exclusions> 77 <exclusion> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-core</artifactId> 80 </exclusion> 81 </exclusions> 82 </dependency> 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-context-support</artifactId> 86 <version>${spring.version}</version> 87 <exclusions> 88 <exclusion> 89 <groupId>org.springframework</groupId> 90 <artifactId>spring-core</artifactId> 91 </exclusion> 92 </exclusions> 93 </dependency> 94 <dependency> 95 <groupId>org.springframework</groupId> 96 <artifactId>spring-beans</artifactId> 97 <version>${spring.version}</version> 98 <exclusions> 99 <exclusion> 100 <groupId>org.springframework</groupId> 101 <artifactId>spring-core</artifactId> 102 </exclusion> 103 </exclusions> 104 </dependency> 105 <dependency> 106 <groupId>org.springframework</groupId> 107 <artifactId>spring-test</artifactId> 108 <version>${spring.version}</version> 109 <exclusions> 110 <exclusion> 111 <groupId>org.springframework</groupId> 112 <artifactId>spring-core</artifactId> 113 </exclusion> 114 </exclusions> 115 </dependency> 116 <dependency> 117 <groupId>org.springframework</groupId> 118 <artifactId>spring-web</artifactId> 119 <version>${spring.version}</version> 120 <exclusions> 121 <exclusion> 122 <groupId>org.springframework</groupId> 123 <artifactId>spring-core</artifactId> 124 </exclusion> 125 </exclusions> 126 </dependency> 127 128 <!-- Spring end --> 129 130 <!-- SpringMvc start --> 131 <dependency> 132 <groupId>org.springframework</groupId> 133 <artifactId>spring-webmvc</artifactId> 134 <version>${spring.version}</version> 135 <exclusions> 136 <exclusion> 137 <groupId>org.springframework</groupId> 138 <artifactId>spring-core</artifactId> 139 </exclusion> 140 </exclusions> 141 </dependency> 142 <dependency> 143 <groupId>org.aspectj</groupId> 144 <artifactId>aspectjweaver</artifactId> 145 <version>1.8.2</version> 146 </dependency> 147 <!-- SpringMvc end --> 148 149 <!-- MyBatis start --> 150 <dependency> 151 <groupId>org.mybatis</groupId> 152 <artifactId>mybatis</artifactId> 153 <version>3.1.1</version> 154 </dependency> 155 <dependency> 156 <groupId>org.mybatis</groupId> 157 <artifactId>mybatis-spring</artifactId> 158 <version>1.2.3</version> 159 </dependency> 160 <dependency> 161 <groupId>org.mybatis</groupId> 162 <artifactId>mybatis-ehcache</artifactId> 163 <version>1.0.0</version> 164 </dependency> 165 <!-- MyBatis end --> 166 167 <dependency> 168 <groupId>org.apache.commons</groupId> 169 <artifactId>commons-collections4</artifactId> 170 <version>4.1</version> 171 </dependency> 172 <dependency> 173 <groupId>log4j</groupId> 174 <artifactId>log4j</artifactId> 175 <version>1.2.17</version> 176 </dependency> 177 <dependency> 178 <groupId>org.slf4j</groupId> 179 <artifactId>slf4j-log4j12</artifactId> 180 <version>1.7.7</version> 181 </dependency> 182 <dependency> 183 <groupId>org.slf4j</groupId> 184 <artifactId>slf4j-api</artifactId> 185 <version>1.7.7</version> 186 </dependency> 187 188 189 190 <!-- JDBC MySQL Driver --> 191 <dependency> 192 <groupId>mysql</groupId> 193 <artifactId>mysql-connector-java</artifactId> 194 <version>5.1.21</version> 195 </dependency> 196 197 <!-- Collections --> 198 <dependency> 199 <groupId>commons-collections</groupId> 200 <artifactId>commons-collections</artifactId> 201 <version>3.2</version> 202 </dependency> 203 <dependency> 204 <groupId>commons-fileupload</groupId> 205 <artifactId>commons-fileupload</artifactId> 206 <version>1.3.1</version> 207 </dependency> 208 <dependency> 209 <groupId>commons-io</groupId> 210 <artifactId>commons-io</artifactId> 211 <version>2.4</version> 212 </dependency> 213 <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> 214 <version>2.5</version> </dependency> --> 215 216 <dependency> 217 <groupId>commons-logging</groupId> 218 <artifactId>commons-logging</artifactId> 219 <version>1.1.3</version> 220 </dependency> 221 <dependency> 222 <groupId>javax.servlet</groupId> 223 <artifactId>servlet-api</artifactId> 224 <version>2.5</version> 225 </dependency> 226 <!-- <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> 227 <version>4.1.2</version> </dependency> --> 228 <!-- <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> 229 <version>1.2</version> </dependency> --> 230 </dependencies> 231 232 <build> 233 <plugins> 234 <plugin> 235 <groupId>org.apache.maven.plugins</groupId> 236 <artifactId>maven-war-plugin</artifactId> 237 <version>2.6</version> 238 <configuration> 239 <failOnMissingWebXml>true</failOnMissingWebXml> 240 </configuration> 241 </plugin> 242 243 </plugins> 244 </build> 245 246 247 248 </project>
2.web.xml --
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > 3 <web-app> 4 <!-- 对标签有顺序要求 --> 5 <!-- 初始化应用上下文 --> 6 <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring.xml</param-value> 7 </context-param> --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath*:/spring.xml</param-value> 11 </context-param> 12 13 <filter> 14 <filter-name>characterEncodingFilter</filter-name> 15 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 16 <init-param> 17 <param-name>encoding</param-name> 18 <param-value>utf-8</param-value> 19 </init-param> 20 <init-param> 21 <param-name>forceEncoding</param-name> 22 <param-value>true</param-value> 23 </init-param> 24 </filter> 25 <filter-mapping> 26 <filter-name>characterEncodingFilter</filter-name> 27 <url-pattern>/*</url-pattern> 28 </filter-mapping> 29 30 <listener> 31 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 32 </listener> 33 34 35 36 37 <servlet> 38 <servlet-name>Spring Mvc Servlet</servlet-name> 39 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 40 <init-param> 41 <param-name>contextConfigLocation</param-name> 42 <param-value>classpath*:/spring-mvc.xml</param-value> 43 </init-param> 44 <load-on-startup>2</load-on-startup> 45 </servlet> 46 <servlet-mapping> 47 <servlet-name>Spring Mvc Servlet</servlet-name> 48 <url-pattern>/</url-pattern> 49 </servlet-mapping> 50 51 52 53 54 55 56 57 <!-- 附:web.xml标签放置顺序 58 1. icon? 59 2. display-name? 60 3. description? 61 4. distributable? 62 5. context-param* 63 6. filter* 64 7. filter-mapping* 65 8. listener* 66 9. servlet* 67 10.servlet-mapping* 68 11.session-config? 69 12.mime-mapping* 70 13.welcome-file-list? 71 14.error-page* 72 15.taglib* 73 16.resource-env-ref* 74 17.resource-ref* 75 18.security-constraint* 76 19.login-config? 77 20.security-role* 78 21.env-entry* 79 22.ejb-ref* 80 23.ejb-local-ref 81 "?"表示该标签出现次数最多为1,"*"表示可以多次出现。 --> 82 83 84 </web-app>
3.spring.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-4.0.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 16 http://www.springframework.org/schema/cache 17 http://www.springframework.org/schema/cache/spring-cache.xsd 18 "> 19 20 21 22 23 24 25 26 </beans>
4.spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 "> 10 11 <context:component-scan base-package="com.swagger.controller" 12 use-default-filters="true"> 13 </context:component-scan> 14 15 <!-- 配置视图解析器 --> 16 17 <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/views/"></property> 19 <property name="suffix" value=".jsp"></property> 20 </bean> --> 21 22 <!-- Enables swgger ui--> 23 <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> 24 <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/> 25 26 <context:annotation-config></context:annotation-config> 27 <mvc:annotation-driven></mvc:annotation-driven> 28 29 <mvc:default-servlet-handler/> 30 </beans>
5.swagger2的配置类、
1 package com.swagger.controller.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.ComponentScan; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 7 8 import springfox.documentation.builders.PathSelectors; 9 import springfox.documentation.builders.RequestHandlerSelectors; 10 import springfox.documentation.service.ApiInfo; 11 import springfox.documentation.spi.DocumentationType; 12 import springfox.documentation.spring.web.plugins.Docket; 13 import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 15 @Configuration 16 @EnableWebMvc 17 @EnableSwagger2 18 @ComponentScan(basePackages="com.swagger.controller") 19 public class SwaggerConfig { 20 21 @Bean 22 public Docket api(){ 23 return new Docket(DocumentationType.SWAGGER_2) 24 .apiInfo(this.apiInfo()) 25 .select() 26 .apis(RequestHandlerSelectors.basePackage("com.swagger.controller")) 27 .paths(PathSelectors.any()) 28 .build(); 29 } 30 31 32 private ApiInfo apiInfo(){ 33 @SuppressWarnings("deprecation") 34 ApiInfo info=new ApiInfo( 35 "Spring 构建RestFule", 36 "aaa", 37 "aa", 38 "a", 39 "cc", 40 "x", 41 "x"); 42 return info; 43 } 44 }
6.测试类
1 package com.swagger.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 import io.swagger.annotations.Api; 9 import io.swagger.annotations.ApiOperation; 10 11 @Api("测试类") 12 @Controller 13 public class SwaggerController { 14 15 @ApiOperation(value="测试方法",httpMethod="GET") 16 @RequestMapping(value="/test",method=RequestMethod.GET) 17 @ResponseBody 18 public String hello(){ 19 return "hello word!!!"; 20 } 21 22 }
7.pom.xml文件中缺少spring的核心包,需要手动加一下
8.效果图
标签:res client encoding commons sch 整合 model aging 资源
原文地址:http://www.cnblogs.com/nihaofenghao/p/7633018.html