(一)SpringBoot接口Http协议开发实战

1、SpringBoot2.xHTTP请求配置讲解

SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧

1、@RestController and @RequestMapping是springMVC的注解,不是springboot特有的

2、@RestController = @Controller+@ResponseBody

3、@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan

2、SpringBoot基础HTTP接口GET请求实战

1、GET请求

(1)单一参数

@RequestMapping(path = "/{id}", method = RequestMethod.GET)

public String getUser(@PathVariable String id ) {}

(2)多个参数(功能描述:测试restful协议,从路径中获取字段)

@RequestMapping(path = "/{city_id}/{user_id}", method = RequestMethod.GET)

public Object findUser(@PathVariable("city_id") String cityId,@PathVariable("user_id") String userId ){

    params.clear();

     params.put("cityId", cityId);

    params.put("userId", userId);

    return params;

}

(3)GetMapping(****/*****)

a.)获取传递的参数

@GetMapping(value="/v1/page_user1")

public Object pageUser(int  from,int size ){

    params.clear();

    params.put("from", from);

    params.put("size", size);

    return params;

}

b.)获取默认值传递的参数

@GetMapping(value="/v1/page_user2")

public Object pageUserV2(@RequestParam(defaultValue="0",name="page")int  from,int size ){

    params.clear();

    params.put("from", from);

    params.put("size", size);

    return params;

}

c.)bean对象传参(RequestBody与RequestMapping 整合,

 注意:

        1、注意需要指定http头为 content-type为application/json

         2、使用body传输数据

@RequestMapping("/v1/save_user")

public Object saveUser(@RequestBody User user){

    params.clear();

    params.put("user", user);

    return params;

}

d.)获取传参消息头

@GetMapping("/v1/get_header")

public Object getHeader(@RequestHeader("access_token") String accessToken, String id){

    params.clear();

    params.put("access_token", accessToken);

    params.put("id", id);

    return params;

}

e.)获取注入的参数

@GetMapping("/v1/test_request")

public Object testRequest(HttpServletRequest request){

    params.clear();

    String id = request.getParameter("id");

    params.put("id", id);

    return params;

}

2.http协议的post,del,put请求

@RestController

public class OtherHttpController {

private Map params = new HashMap<>();

@PostMapping("/v1/login")

public Object login(String id, String pwd){

    params.clear();

    params.put("id", id);

    params.put("pwd", pwd);

    return params;

}

@PutMapping("/v1/put")

public Object put(String id){

    params.clear();

    params.put("id", id);

    return params;

}

@DeleteMapping("/v1/del")

public Object del(String id){

    params.clear();

    params.put("id", id);

    return params;

}

}

3.Jackson

指定字段不返回:@JsonIgnore

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

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

指定别名:@JsonProperty

public class User {

    private int age;

    @JsonIgnore

    private String pwd;

    @JsonProperty("account")

    @JsonInclude(Include.NON_NULL)

    private String phone;

    @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")

    private Date createTime;

    public User() {

        super();

    }

    public User(int age, String pwd, String phone, Date createTime) {

        super();

        this.age = age;

        this.pwd = pwd;

        this.createTime = createTime;

    }

}

4.目录文件结构

1、目录讲解

    src/main/java:存放代码

    src/main/resources

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

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

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

    resources:

2、引入依赖 Thymeleaf

    

           org.springframework.boot

       spring-boot-starter-thymeleaf

    

注意:如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,

比如resources、static、public这个几个文件夹,才可以访问

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

Spring Boot 默认会挨个从

META/resources > resources > static > public  里面找是否存在相应的资源,如果有则直接返回。

4、默认配置

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

备注:

application.properties 加 “,classpath:/test/” 代表加载后会扫描这个

pom 加thymeleaf  会自动扫描 templates/*.html 文件 用@Controller 映射过去,返页面跳转。

你可能感兴趣的:((一)SpringBoot接口Http协议开发实战)