Spring Boot 请求方式和访问静态页面

  • Post author:
  • Post category:其他


Spring Boot 请求方式:

1.get请求

@RequestMapping(value = "/findUser",method = RequestMethod.GET)
    public UserInfo findUser(){
        return new UserInfo("1","张三","123","男","22");
    }

2.restful风格

@GetMapping(value = "/rest/{id}/{name}")
    public Map findObject(@PathVariable(value = "id") String id, @PathVariable(value = "name") String name){
        map.clear();
        map.put("id",id);
        map.put("name",name);
        return map;
    }

3.分页列表

    @GetMapping(value = "/page")
    public Map pages(@RequestParam(name = "page",defaultValue = "1")Integer page,Integer limit){
        map.clear();
        map.put("page",page);
        map.put("limit",limit);
        return map;
    }

4.添加数据


    @PostMapping(value = "/add")
    public Map addUser(UserInfo ui){
        map.clear();
        map.put("ui",ui);
        return map;
    }

5.token形式

    @GetMapping(value = "/findheader")
    public Map findHeader(@RequestHeader("token")String token,String id){
        map.clear();
        map.put("token",token);
        map.put("id",id);
        return map;
    }

6.查找信息

    @GetMapping(value = "/find")
    public Map findHeader(HttpServletRequest request){
        map.clear();
        String id = request.getParameter("id");
        map.put("id",id);
        return map;
    }

7.登录

    @PostMapping(value = "/login")
    public Map login(String name ,String pwd){
        map.clear();
        map.put("name",name);
        map.put("pwd",pwd);
        return map;
    }

8.修改信息

    @PutMapping(value = "/update")
    public Map update(String name){
        map.clear();
        map.put("name",name);
        return map;
    }

9.删除信息

    @DeleteMapping(value = "/delete")
    public Map delete(String id){
        map.clear();
        map.put("id",id);
        return map;
    }


常用框架阿里fastjson, 谷歌gson

JavaBean序列化为Json,性能: Jackson > FastJson > Gson > Json-lib 同个结构

Jackson、FastJson、 Gson类库各有优点,各有自己的专长

空间换时间,时间换空间


Jackson处理相关自动

指定字段不返回: @JsonIgnore

指定日期格式: @JsonFormat (pattern=”yyy-MM-dd hh:mm:ss” , locale=” zh” , timezone=”GMT+8″ )

空字段不返回: @JsonInclude(Include . NON_NULL)

指定别名: @JsonProperty


SpringBoot目录文件结构

1、目录讲解

src/main/java:存放代码

src/main/resources

static:存放静态文件,比如css、 js、 image, (访问方式 http://localhost:8080/js/xxx.js)

templates:存放静态页面jsp, html,tpl

config:存放配置文件, application. properties

resources:

2.引入依赖 Thymelesf

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>

如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,比如resources、static. public这个几个文件夹,才可以访问。

3、同个文件的加载顺序,静态资源文件

Spring Boot 默认会挨个从META/resources > resources > static > public里面找是 否存在相应的资

源,如果有则直接返回。

4、默认配置

1)官网地址: https ://docs。spring. io/spring-boot/docs/current/reference/html/

boot-features -developing-web- applications . html#boot – features- spring-mvc-static-content

2) spring.resources. static-locations = classpath: /META- INF/resources/ , classpath: /resources/ ,classpath:/static/ , classpath:/public/

Spring Boot访问静态资源

在浏览器中直接输入

localhost:8080/a.html

会去访问

static

文件夹下的静态资源

如果想要访问到

templates

下的静态资源有两种方式,但是都要

在pom.xml

里面去

引入Thymelesf依赖。

1)在application.properties里面添加以下配置定死

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

2)在application.properties里面添加以下配置进行灵活访问

spring.resources.static-locations = classpath:classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/page/

最后在Controller控制层中进行访问

package org.zhx.bootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

    @GetMapping("/finds")
    public String find(){
        return "a";
    }

    @GetMapping("/findss")
    public String finds(){
        return "ab.html";
    }
}



版权声明:本文为m0_64895976原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。