创建一个springboot工程最小化代码(json-lib的引入gradle方式)
compile 'org.springframework.boot:spring-boot-starter-web:1.4.0.RELEASE' compile 'net.sf.json-lib:json-lib:2.4:jdk15' compile 'org.apache.httpcomponents:httpcore:4.4.10' compile 'org.apache.httpcomponents:httpmime:4.5.6' compile 'org.apache.httpcomponents:httpclient:4.5.6'
<!-- maven -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
最小代码的springboot工程:
settings.gradle:
/* * This settings file was generated by the Gradle 'init' task. * * The settings file is used to specify which projects to include in your build. * In a single project build this file can be empty or even removed. * * Detailed information about configuring a multi-project build in Gradle can be found * in the user guide at https://docs.gradle.org/4.3/userguide/multi_project_builds.html */ /* // To declare projects as part of a multi-project build use the 'include' method include 'shared' include 'api' include 'services:webservice' */ rootProject.name = 'testcase'
build.gradle
apply plugin: 'java-library' apply plugin: 'org.springframework.boot' buildscript { repositories { maven { name 'Aliyun Maven Repository' url "http://maven.aliyun.com/nexus/content/groups/public/" } } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.18.RELEASE" classpath "io.spring.gradle:dependency-management-plugin:1.0.5.RELEASE" } } // In this section you declare where to find the dependencies of your project repositories { maven { name 'Aliyun Maven Repository' url "http://maven.aliyun.com/nexus/content/groups/public/" } jcenter() } springBoot { executable = true mainClass = 'com.casetest.TestCaseBoot' } dependencies { //Spring boot compile 'org.springframework.boot:spring-boot-starter-web:1.5.18.RELEASE' // Swagger2 compile 'io.github.swagger2markup:swagger2markup:1.3.3' compile 'io.springfox:springfox-swagger2:2.9.0' compile 'io.springfox:springfox-swagger-ui:2.9.0' api 'org.apache.commons:commons-math3:3.6.1' implementation 'com.google.guava:guava:23.0' testImplementation 'junit:junit:4.12' }
src下的文件:
package com.casetest; import java.io.IOException; import java.util.ArrayList; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @SpringBootApplication @ServletComponentScan //启动器启动时,扫描本目录以及子目录带有的webservlet注解的 public class TestCaseBoot { public static void main(String[] args) throws IOException { SpringApplication.run(TestCaseBoot.class, args); System.out.println("success"); } /*@Bean // 一定要加,不然这个方法不会运行 // 一定要返回ServletRegistrationBean public ServletRegistrationBean getServletRegistrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new CaseTwoServlet()); bean.addUrlMappings("/secondServlet"); // 访问路径值 return bean; }*/ @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2).groupName("agent").select() .apis(RequestHandlerSelectors.basePackage("com.casetest")) .paths(PathSelectors.any()) .paths(PathSelectors.ant("/api/**/*")) .build() .apiInfo(new ApiInfo("API", "这App等相关的API", "v3", "Terms of service", new Contact("Test", "http://www.testcase.com", ""), "", "", new ArrayList<>())); } }
package com.casetest.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration @ComponentScan({ "com.casetest.rs", "com.casetest.schedule", "com.casetest.service" }) public class WebAutoConfiguration { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
package com.casetest.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfiguration extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") .maxAge(3600) .allowCredentials(true); } }
package com.casetest.rs; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/api/case/one") @RestController public class CaseOneRS { @RequestMapping(value="/sayHi",method= {RequestMethod.GET}) public String sayHi(String name) { return "Hi " + name; } @RequestMapping(value="/sayHi",method= {RequestMethod.POST}) public String replyHi(String name) { return "me to " + name; } }
package com.casetest.rs; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns="/api/case/two",loadOnStartup=1) public class CaseTwoServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("get...."); super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("post...."); super.doPost(req, resp); } }