创建一个springboot工程最小化代码

简介: 创建一个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'

<groupId>net.sf.json-lib</groupId>    
<artifactId>json-lib</artifactId>    
<version>2.4</version>    
<classifier>jdk15</classifier>    

最小代码的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.

*

*/

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

}

相关文章
|
13天前
|
安全 Java 应用服务中间件
Spring Boot + Java 21:内存减少 60%,启动速度提高 30% — 零代码
通过调整三个JVM和Spring Boot配置开关,无需重写代码即可显著优化Java应用性能:内存减少60%,启动速度提升30%。适用于所有在JVM上运行API的生产团队,低成本实现高效能。
97 3
|
4月前
|
监控 Java 数据安全/隐私保护
阿里面试:SpringBoot启动时, 如何执行扩展代码?你们项目 SpringBoot 进行过 哪些 扩展?
阿里面试:SpringBoot启动时, 如何执行扩展代码?你们项目 SpringBoot 进行过 哪些 扩展?
|
4月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
642 1
Spring boot 使用mybatis generator 自动生成代码插件
|
4月前
|
Java 调度 流计算
基于Java 17 + Spring Boot 3.2 + Flink 1.18的智慧实验室管理系统核心代码
这是一套基于Java 17、Spring Boot 3.2和Flink 1.18开发的智慧实验室管理系统核心代码。系统涵盖多协议设备接入(支持OPC UA、MQTT等12种工业协议)、实时异常检测(Flink流处理引擎实现设备状态监控)、强化学习调度(Q-Learning算法优化资源分配)、三维可视化(JavaFX与WebGL渲染实验室空间)、微服务架构(Spring Cloud构建分布式体系)及数据湖建设(Spark构建实验室数据仓库)。实际应用中,该系统显著提升了设备调度效率(响应时间从46分钟降至9秒)、设备利用率(从41%提升至89%),并大幅减少实验准备时间和维护成本。
264 0
|
8月前
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
1289 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
9月前
|
Java 应用服务中间件
SpringBoot工程打包部署
SpringBoot工程打包部署简介:SpringBoot项目可通过三种方式运行:可执行Jar包、可执行War包和标准War包。其中,可执行Jar/War包可独立运行,标准War包需部署在Tomcat中。具体步骤包括:1. 修改pom.xml添加构建依赖;2. 执行`mvn clean package`命令打包;3. 运行生成的Jar/War包(如`java -jar xxx.jar`)。对于标准War包,还需修改启动类并配置Tomcat依赖。
428 7
|
9月前
|
XML 前端开发 Java
SpringBoot整合Flowable【04】- 通过代码控制流程流转
本文介绍了如何使用Flowable的Java API控制流程流转,基于前文构建的绩效流程模型。首先,通过Flowable-UI导出模型文件并部署到Spring Boot项目中。接着,详细讲解了如何通过代码部署、启动和审批流程,涉及`RepositoryService`、`RuntimeService`和`TaskService`等核心服务类的使用。最后,通过实际操作演示了流程从部署到完成的全过程,并简要说明了相关数据库表的变化。本文帮助读者初步掌握Flowable在实际业务中的应用,后续将深入探讨更多高级功能。
1144 0
SpringBoot整合Flowable【04】-  通过代码控制流程流转
|
12月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
473 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
12月前
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
2558 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
|
12月前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
2548 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个