【Java快速入门】-- 基于SpringBoot的web开发框架

快速使用spring-boot来构建web应用

java基础及mvn创建应用

重要概念

  • 什么是注解:带有@符号的就是注解,注解起一个解释标识作用,程序会根据反射来找对应的函数
  • 什么是javaBean对象:在spring框架中,带有javaBean注解的类将被自动实例化,无序用户在new操作,对于单例模式时非常友好的
  • 程序如何执行的:在spring中,程序的执行会根据注解反射对象进行实例化,也就是程序会不断根据注解来自动化实例对象

第一步:创建项目

$ mvn archetype:generate
...
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
...
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
...
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: # 选择默认即可
Define value for property 'groupId': com.xxx # 一般情况使用公司域名
Define value for property 'artifactId': lijc # 定义项目名称
Define value for property 'version' 1.0-SNAPSHOT: : # 定义版本
Define value for property 'package' com.xxx: : # 定义包名
$ tree
.
├── pom.xml # mvn包管理配置文件
├── readme.md
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── xxx
    │               └── App.java # 得到的主程序运行文件
    └── test
        └── java
            └── com
                └── xxx
                    └── AppTest.java # 得到的主程序测试文件

第二步:安装依赖

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.10version>
  parent>

<dependencies>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
    
    <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
    <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.47version>
    dependency>

    
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger2artifactId>
        <version>3.0.0version>
    dependency>
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger-uiartifactId>
        <version>3.0.0version>
    dependency>
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-boot-starterartifactId>
        <version>3.0.0version>
    dependency>
dependencies>

第三步:构建业务代码

添加配置文件

  • 在src/main/resources/application.yml
server:
  port: 8090 #服务端口
  compression: #开启数据压缩
    enabled: true
    min-response-size: 1024
    mime-types: application/json

info:
  version: 1.2

定义返回响应体

/**
 * 这种数据基本由代码自动生成getter与setter
 */
@Component // 定义一个javaBean对象,定义一个返回格式
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 配置该bean对象为多例模式
class ResultResponse {

    @Override
    public String toString() {
        return "{" +
                " version='" + getVersion() + "'" +
                ", code='" + getCode() + "'" +
                ", msg='" + getMsg() + "'" +
                ", data='" + getData() + "'" +
                "}";
    }

    // 以下代码使用vscode插件JAVA CODE GENERATORS自动生成
    @Value("${info.version}") // 该参数将从配置文件application.yml中获取
    private String version;
    private Integer code;
    private String msg;
    private Object data;

    public String getVersion() {
        return this.version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResultResponse() {
    }

}

配置swagger接口文档

// 配置接口文档,以下为固定写法
@Configuration
@EnableSwagger2
class SwaggerConfig {
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("API接口文档")
                .description("使用swagger来测试接口文档")
                .version("1.0.0")
                .build();
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxx")) // 这里写的是API接口所在的包位置

                .paths(PathSelectors.any())
                .build();
    }
}

定义一个业务接口


// 定义一个接口
@RestController // 声明是一个javaBean对象
@Api(value = "Hello接口") // swagger 配置
class Hello2 {
    // @Value("${info.version}")
    // private String version;

    /**
     * @Autowired // 自动注解
     */
    @Autowired
    private ResultResponse rresp;

    /**
     * 
     * @RequestMapping // 设置路由
     * @RequestParam // 这是请求参数,并附默认值
     */
    @RequestMapping(method = RequestMethod.POST, value = "/hello2/{id}")
    @ApiOperation(value = "测试post请求", notes = "测试post请求") // swagger 配置
    public ResultResponse hello2(
            @RequestParam(defaultValue = "jack", name = "name") String name,
            @RequestPart("file") MultipartFile file,
            @PathVariable(value = "id") Integer id,
            @RequestParam(name = "age") Integer age) {

        Map<String, Object> m = new HashMap<String, Object>();
        // m.put("name", name);
        m.put("age", age);
        m.put("id", id);
        rresp.setCode(200);
        rresp.setMsg("成功");
        rresp.setData(m);
        System.out.println(rresp);
        return rresp;
    }
}


针对实际业务设计拦截器


// 定义一个拦截器
@Component
class MyInterceptor implements HandlerInterceptor {

    @Autowired
    private ResultResponse rresp;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("你的请求被拦截,需要审核是否放行");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(403);
        rresp.setCode(403);
        rresp.setMsg("被拦截");
        rresp.setData("");
        JSONObject resp = (JSONObject) JSONObject.toJSON(rresp);
        response.getWriter().println(resp.toJSONString());
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

构建主函数


/**
 * Hello world!
 * 
 * @EnableAutoConfiguration 自动加入application配置
 */
@Configuration
@SpringBootApplication // 声明javabean配置文件
@ComponentScan("com.xxxx") // 自动扫描bean对象
@EnableAutoConfiguration // 自动扫描配置文件
public class App implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // TODO Auto-generated method stub
        // 这里配置拦截路径
        registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(
                "/hello2/**",
                "/swagger-ui/**",
                "/v2/api-docs",
                "/swagger-resources/**",
                "/css/**",
                "/js/**");
        // WebMvcConfigurer.super.addInterceptors(registry);
    }

    public static void main(String[] args) {
        // System.out.println("Hello World!");
        SpringApplication.run(App.class, args); // 自动去扫描注解为RequestMapping的接口

    }
}

运行

$ mvn spring-boot:run
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.5.10)

2022-03-15 14:26:56.849  INFO 3344 --- [  restartedMain] com.xxxx.App                             : Starting App using Java 17.0.2 on rockontroldeMacBook-Pro.local with PID 3344 (/Users/rockontrol/Desktop/js_code/test/java-test/springboot-test-ljc/target/classes started by rockontrol in /Users/rockontrol/Desktop/js_code/test/java-test/springboot-test-ljc)
2022-03-15 14:26:56.850  INFO 3344 --- [  restartedMain] com.xxxx.App                             : No active profile set, falling back to 1 default profile: "default"
2022-03-15 14:26:57.040  INFO 3344 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8090 (http)
2022-03-15 14:26:57.043  INFO 3344 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-03-15 14:26:57.044  INFO 3344 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.58]
2022-03-15 14:26:57.054  INFO 3344 --- [  restartedMain] o.a.c.c.C.[Tomcat-1].[localhost].[/]     : Initializing Spring embedded WebApplicationContext
2022-03-15 14:26:57.055  INFO 3344 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 200 ms
2022-03-15 14:26:57.129  INFO 3344 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-03-15 14:26:57.137  INFO 3344 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8090 (http) with context path ''
2022-03-15 14:26:57.140  INFO 3344 --- [  restartedMain] com.xxxx.App                             : Started App in 5.34 seconds (JVM running for 5056.503)
2022-03-15 14:26:57.143  INFO 3344 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged

效果

  • 访问地址:http://127.0.0.1:8090/swagger-ui/index.html
    【Java快速入门】-- 基于SpringBoot的web开发框架_第1张图片

完整代码如下

package com.xxxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import org.springframework.stereotype.Component;

/**
 * 这种数据基本由代码自动生成getter与setter
 */
@Component // 定义一个javaBean对象,定义一个返回格式
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 配置该bean对象为多例模式
class ResultResponse {

    @Override
    public String toString() {
        return "{" +
                " version='" + getVersion() + "'" +
                ", code='" + getCode() + "'" +
                ", msg='" + getMsg() + "'" +
                ", data='" + getData() + "'" +
                "}";
    }

    // 以下代码使用vscode插件JAVA CODE GENERATORS自动生成
    @Value("${info.version}") // 该参数将从配置文件application.yml中获取
    private String version;
    private Integer code;
    private String msg;
    private Object data;

    public String getVersion() {
        return this.version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResultResponse() {
    }

}

// 配置接口文档,以下为固定写法
@Configuration
@EnableSwagger2
class SwaggerConfig {
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("API接口文档")
                .description("使用swagger来测试接口文档")
                .version("1.0.0")
                .build();
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxx")) // 这里写的是API接口所在的包位置

                .paths(PathSelectors.any())
                .build();
    }
}

// 定义一个接口
@RestController // 声明是一个javaBean对象
@Api(value = "Hello接口") // swagger 配置
class Hello2 {
    // @Value("${info.version}")
    // private String version;

    /**
     * @Autowired // 自动注解
     */
    @Autowired
    private ResultResponse rresp;

    /**
     * 
     * @RequestMapping // 设置路由
     * @RequestParam // 这是请求参数,并附默认值
     */
    @RequestMapping(method = RequestMethod.POST, value = "/hello2/{id}")
    @ApiOperation(value = "测试post请求", notes = "测试post请求") // swagger 配置
    public ResultResponse hello2(
            @RequestParam(defaultValue = "jack", name = "name") String name,
            @RequestPart("file") MultipartFile file,
            @PathVariable(value = "id") Integer id,
            @RequestParam(name = "age") Integer age) {

        Map<String, Object> m = new HashMap<String, Object>();
        // m.put("name", name);
        m.put("age", age);
        m.put("id", id);
        rresp.setCode(200);
        rresp.setMsg("成功");
        rresp.setData(m);
        System.out.println(rresp);
        return rresp;
    }
}

// 定义一个拦截器
@Component
class MyInterceptor implements HandlerInterceptor {

    @Autowired
    private ResultResponse rresp;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("你的请求被拦截,需要审核是否放行");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(403);
        rresp.setCode(403);
        rresp.setMsg("被拦截");
        rresp.setData("");
        JSONObject resp = (JSONObject) JSONObject.toJSON(rresp);
        response.getWriter().println(resp.toJSONString());
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

/**
 * Hello world!
 * 
 * @EnableAutoConfiguration 自动加入application配置
 */
@Configuration
@SpringBootApplication // 声明javabean配置文件
@ComponentScan("com.xxxx") // 自动扫描bean对象
@EnableAutoConfiguration // 自动扫描配置文件
public class App implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // TODO Auto-generated method stub
        // 这里配置拦截路径
        registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(
                "/hello2/**",
                "/swagger-ui/**",
                "/v2/api-docs",
                "/swagger-resources/**",
                "/css/**",
                "/js/**");
        // WebMvcConfigurer.super.addInterceptors(registry);
    }

    public static void main(String[] args) {
        // System.out.println("Hello World!");
        SpringApplication.run(App.class, args); // 自动去扫描注解为RequestMapping的接口

    }
}

你可能感兴趣的:(java,java,spring,boot,前端)