- 创建Maven 项目
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="schedule-console" version="3.0"> 6 <display-name>Archetype Created Web Application</display-name> 7 <context-param> 8 <param-name>log4jConfigLocation</param-name> 9 <param-value>/WEB-INF/log4j.xml</param-value> 10 </context-param> 11 <servlet> 12 <servlet-name>springmvc</servlet-name> 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 14 <load-on-startup>1</load-on-startup> 15 </servlet> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 <listener> 20 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 21 </listener> 22 <filter> 23 <filter-name>encodingFilter</filter-name> 24 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 25 <init-param> 26 <param-name>encoding</param-name> 27 <param-value>UTF-8</param-value> 28 </init-param> 29 <init-param> 30 <param-name>forceEncoding</param-name> 31 <param-value>true</param-value> 32 </init-param> 33 </filter> 34 <filter-mapping> 35 <filter-name>encodingFilter</filter-name> 36 <url-pattern>/*</url-pattern> 37 </filter-mapping> 38 39 <servlet-mapping> 40 <servlet-name>default</servlet-name> 41 <url-pattern>*.css</url-pattern> 42 </servlet-mapping> 43 44 <servlet-mapping> 45 <servlet-name>default</servlet-name> 46 <url-pattern>*.json</url-pattern> 47 </servlet-mapping> 48 49 <servlet-mapping> 50 <servlet-name>default</servlet-name> 51 <url-pattern>*.gif</url-pattern> 52 </servlet-mapping> 53 54 <servlet-mapping> 55 <servlet-name>default</servlet-name> 56 <url-pattern>*.jpg</url-pattern> 57 </servlet-mapping> 58 59 <servlet-mapping> 60 <servlet-name>default</servlet-name> 61 <url-pattern>*.js</url-pattern> 62 </servlet-mapping> 63 64 <servlet-mapping> 65 <servlet-name>default</servlet-name> 66 <url-pattern>*.html</url-pattern> 67 </servlet-mapping> 68 69 <servlet-mapping> 70 <servlet-name>springmvc</servlet-name> 71 <url-pattern>/</url-pattern> 72 </servlet-mapping> 73 <welcome-file-list> 74 <welcome-file>index.html</welcome-file> 75 <welcome-file>index.jsp</welcome-file> 76 </welcome-file-list> 77 </web-app>
Pojo:定义DTO、VO、Enum、Entities等公共的实体对象
Service:定义API服务接口
Core:具体的API功能实现
Web: API Controller层实现
Deploy:webapp 服务
- Maven 依赖
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.martin</groupId> 6 <artifactId>pay-parent</artifactId> 7 <version>1.0.0-SNAPSHOT</version> 8 <packaging>pom</packaging> 9 <properties> 10 <org.springframework-version>4.3.7.RELEASE</org.springframework-version> 11 </properties> 12 <dependencies> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>3.8.1</version> 17 <scope>test</scope> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-context</artifactId> 22 <version>${org.springframework-version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context-support</artifactId> 27 <version>${org.springframework-version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-core</artifactId> 32 <version>${org.springframework-version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-beans</artifactId> 37 <version>${org.springframework-version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-web</artifactId> 42 <version>${org.springframework-version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-webmvc</artifactId> 47 <version>${org.springframework-version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-tx</artifactId> 52 <version>${org.springframework-version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-jdbc</artifactId> 57 <version>${org.springframework-version}</version> 58 </dependency> 59 <dependency> 60 <groupId>org.springframework</groupId> 61 <artifactId>spring-orm</artifactId> 62 <version>${org.springframework-version}</version> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-aop</artifactId> 67 <version>${org.springframework-version}</version> 68 </dependency> 69 <dependency> 70 <groupId>org.aspectj</groupId> 71 <artifactId>aspectjrt</artifactId> 72 <version>1.8.10</version> 73 </dependency> 74 <dependency> 75 <groupId>org.aspectj</groupId> 76 <artifactId>aspectjweaver</artifactId> 77 <version>1.8.10</version> 78 </dependency> 94 <dependency> 95 <groupId>org.mybatis</groupId> 96 <artifactId>mybatis</artifactId> 97 <version>3.2.1</version> 98 </dependency> 99 <dependency> 100 <groupId>org.bgee.log4jdbc-log4j2</groupId> 101 <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId> 102 <version>1.16</version> 103 </dependency> 104 <dependency> 105 <groupId>org.slf4j</groupId> 106 <artifactId>slf4j-api</artifactId> 107 <version>1.7.13</version> 108 </dependency> 109 <dependency> 110 <groupId>org.slf4j</groupId> 111 <artifactId>slf4j-log4j12</artifactId> 112 <version>1.7.13</version> 113 </dependency> 114 <dependency> 115 <groupId>com.fasterxml.jackson.core</groupId> 116 <artifactId>jackson-databind</artifactId> 117 <version>2.8.8</version> 118 </dependency> 119 </dependencies> 120 <modules> 121 <module>pay-pojo</module> 122 <module>pay-service</module> 123 <module>pay-core</module> 124 <module>pay-web</module> 125 <module>pay-deploy</module> 126 <module>pay-common</module> 127 </modules> 128 </project>
- Deploy中web.xml配置
更新deploy web module 为3.0版
Springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 11 <context:annotation-config /> 12 <aop:aspectj-autoproxy proxy-target-class="true" /> 13 <context:component-scan base-package="com.martin.pay" /> 14 15 <mvc:annotation-driven> 16 <mvc:message-converters register-defaults="false"> 17 <bean class="com.martin.pay.common.PayJsonHttpMessageConvert" 18 init-method="initFormat" /> 19 <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> 20 </mvc:message-converters> 21 </mvc:annotation-driven> 22 </beans>
- Log4j.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 4 <!-- APPENDER --> 5 <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> 6 <param name="Target" value="System.out" /> 7 <layout class="org.apache.log4j.PatternLayout"> 8 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 9 </layout> 10 </appender> 11 <appender name="ALL1" class="org.apache.log4j.DailyRollingFileAppender"> 12 <param name="file" value="../logs/all.log" /> 13 <param name="append" value="true" /> 14 <param name="encoding" value="UTF-8" /> 15 <param name="datePattern" value=".yyyy-MM-dd" /> 16 <layout class="org.apache.log4j.PatternLayout"> 17 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 18 </layout> 19 </appender> 20 <appender name="WEB" class="org.apache.log4j.DailyRollingFileAppender"> 21 <param name="file" value="../logs/web.log" /> 22 <param name="append" value="true" /> 23 <param name="encoding" value="UTF-8" /> 24 <layout class="org.apache.log4j.PatternLayout"> 25 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 26 </layout> 27 </appender> 28 <appender name="CORE" class="org.apache.log4j.DailyRollingFileAppender"> 29 <param name="file" value="../logs/core.log" /> 30 <param name="append" value="true" /> 31 <param name="encoding" value="UTF-8" /> 32 <layout class="org.apache.log4j.PatternLayout"> 33 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 34 </layout> 35 </appender> 36 <appender name="TASK" class="org.apache.log4j.DailyRollingFileAppender"> 37 <param name="file" value="../logs/task.log" /> 38 <param name="append" value="true" /> 39 <param name="encoding" value="UTF-8" /> 40 <layout class="org.apache.log4j.PatternLayout"> 41 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 42 </layout> 43 </appender> 44 <appender name="EXCEPTION_LOG" class="org.apache.log4j.DailyRollingFileAppender"> 45 <param name="Threshold" value="ERROR" /> 46 <param name="file" value="../logs/error.log" /> 47 <param name="append" value="true" /> 48 <param name="encoding" value="UTF-8" /> 49 <layout class="org.apache.log4j.PatternLayout"> 50 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 51 </layout> 52 </appender> 53 54 <!-- PROFILER用于打内部执行时间的 --> 55 <appender name="PROFILER" class="org.apache.log4j.DailyRollingFileAppender"> 56 <param name="file" value="../logs/profile.log" /> 57 <param name="append" value="true" /> 58 <param name="encoding" value="UTF-8" /> 59 <layout class="org.apache.log4j.PatternLayout"> 60 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 61 </layout> 62 </appender> 63 64 <appender name="InvokeRecorder" class="org.apache.log4j.DailyRollingFileAppender"> 65 <param name="file" value="../logs/invokeRecorder.log" /> 66 <param name="append" value="true" /> 67 <param name="encoding" value="UTF-8" /> 68 <layout class="org.apache.log4j.PatternLayout"> 69 <param name="ConversionPattern" value="%d %X{traceId} %-5p:%L %c{2} - %m%n" /> 70 </layout> 71 </appender> 72 73 <!-- LOGGER --> 74 <logger name="com.martin.pay.web"> 75 <level value="DEBUG" /> 76 <appender-ref ref="WEB" /> 77 </logger> 78 <logger name="com.martin.pay.core"> 79 <level value="DEBUG" /> 80 <appender-ref ref="CORE" /> 81 </logger> 82 <root> 83 <level value="DEBUG" /> 84 <appender-ref ref="ALL1" /> 85 <appender-ref ref="EXCEPTION_LOG" /> 86 </root> 87 </log4j:configuration>
Swagger
1 <build> 2 <finalName>pay-deploy</finalName> 3 <plugins> 4 <plugin> 5 <groupId>com.github.kongchen</groupId> 6 <artifactId>swagger-maven-plugin</artifactId> 7 <version>3.1.0</version> 8 <configuration> 9 <apiSources> 10 <apiSource> 11 <springmvc>true</springmvc> 12 <locations>com.martin.pay.web.controller</locations> 13 <schemes>http</schemes> 14 <host>localhost:9120</host> 15 <basePath>/</basePath> 16 <info> 17 <title>Pay API doc </title> 18 <version>v1</version> 19 <description>Pay service</description> 20 </info> 21 <templatePath> 22 ${basedir}/templates/strapdown.html.hbs 23 </templatePath> 24 <outputPath>${basedir}/src/main/webapp/generated/document.html 25 </outputPath> 26 <swaggerDirectory>${basedir}/src/main/webapp/generated/swagger-ui 27 </swaggerDirectory> 28 </apiSource> 29 </apiSources> 30 </configuration> 31 <executions> 32 <execution> 33 <phase>compile</phase> 34 <goals> 35 <goal>generate</goal> 36 </goals> 37 </execution> 38 </executions> 39 </plugin> 40 </plugins> 41 <pluginManagement> 42 <plugins> 43 <!--This plugin's configuration is used to store Eclipse m2e settings 44 only. It has no influence on the Maven build itself. --> 45 <plugin> 46 <groupId>org.eclipse.m2e</groupId> 47 <artifactId>lifecycle-mapping</artifactId> 48 <version>1.0.0</version> 49 <configuration> 50 <lifecycleMappingMetadata> 51 <pluginExecutions> 52 <pluginExecution> 53 <pluginExecutionFilter> 54 <groupId> 55 com.github.kongchen 56 </groupId> 57 <artifactId> 58 swagger-maven-plugin 59 </artifactId> 60 <versionRange> 61 [3.1.0,) 62 </versionRange> 63 <goals> 64 <goal>generate</goal> 65 </goals> 66 </pluginExecutionFilter> 67 <action> 68 <ignore></ignore> 69 </action> 70 </pluginExecution> 71 </pluginExecutions> 72 </lifecycleMappingMetadata> 73 </configuration> 74 </plugin> 75 </plugins> 76 </pluginManagement> 77 </build>