ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

创建一个springboot工程最小化代码(json-lib的引入gradle方式)

2021-05-18 11:06:29  阅读:172  来源: 互联网

标签:springboot springframework gradle json org import springfox com public


   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);
    }
    
}

 

标签:springboot,springframework,gradle,json,org,import,springfox,com,public
来源: https://www.cnblogs.com/liangblog/p/14779938.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有