HTTP协议,请求响应

、概述

HTTP协议,请求响应_第1张图片

二、HTTP请求协议

HTTP协议,请求响应_第2张图片

HTTP协议,请求响应_第3张图片

三、HTTP响应协议

HTTP协议,请求响应_第4张图片

HTTP协议,请求响应_第5张图片

HTTP协议,请求响应_第6张图片

四、请求数据

1.简单实体参数

HTTP协议,请求响应_第7张图片HTTP协议,请求响应_第8张图片

HTTP协议,请求响应_第9张图片

HTTP协议,请求响应_第10张图片

    @RequestMapping("/simpleParam")
    public  String simpleParam(@RequestParam(name ="name" ,required = false ) String username, Integer age){
        System.out.println (username +":"+age);//自动转换数据类型
        return "OK";
    }

2.实体参数

HTTP协议,请求响应_第11张图片

HTTP协议,请求响应_第12张图片

@RequestMapping("/simplePojo")
public String simplePojo(User user){
    System.out.println ( user );
    return "ok";

3.数组参数

HTTP协议,请求响应_第13张图片

@RequestMapping("/arrayParam")
public String arrayParam(String [] hobby){
    System.out.println ( Arrays.toString ( hobby ) );
    return "ok";

4.集合参数

HTTP协议,请求响应_第14张图片

HTTP协议,请求响应_第15张图片

 @RequestMapping("/listParam")
    public String listParam(@RequestParam  List hobby){
        System.out.println ( hobby );
        return "ok";

    }

5.日期参数

HTTP协议,请求响应_第16张图片

    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){
        System.out.println (updateTime);
        return "ok";
    }

6.Json参数

HTTP协议,请求响应_第17张图片

    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){
        System.out.println (user);
        return "ok";
    }

7.路径参数

HTTP协议,请求响应_第18张图片

    //路径参数
    @RequestMapping("/path/{id}")
    public String pathParam(@PathVariable Integer id){
        System.out.println (id);
        return "ok";
    }


    @RequestMapping("/path/{id}/{name}")
    public String pathParams(@PathVariable Integer id,@PathVariable String name){
        System.out.println (id);
        return "ok";
    }

HTTP协议,请求响应_第19张图片

五、响应数据

HTTP协议,请求响应_第20张图片

package com.itheima.controller;

import com.itheima.pojo.Address;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ResponseController {
    @RequestMapping("hello")
    public String hello(){
        System.out.println ("Hello World ~");
        return "Hello World";
    }
    @RequestMapping("/getAddr")
    public Address getAddr(){
        Address addr=new Address ();
        addr.setProvince ( "广东" );
        addr.setCity ( "深圳" );
        return addr;
    }
    @RequestMapping("/listAddr")
    public List
listAddr(){ List
list =new ArrayList<> (); Address addr=new Address (); addr.setProvince ( "广东" ); addr.setCity ( "西安" ); Address addr2=new Address (); addr2.setProvince ( "陕西" ); addr2.setCity ( "西安" ); list.add ( addr ); list.add ( addr2 ); return list; } }

HTTP协议,请求响应_第21张图片

package com.itheima.pojo;

/**
 * 统一响应结果封装类
 */
public class Result {
    private Integer code ;//1 成功 , 0 失败
    private String msg; //提示信息
    private Object data; //数据 data

    public Result() {
    }
    public Result(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

    public static Result success(Object data){
        return new Result(1, "success", data);
    }
    public static Result success(){
        return new Result(1, "success", null);
    }
    public static Result error(String msg){
        return new Result(0, msg, null);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
import com.itheima.pojo.Address;
import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ResponseController {
    @RequestMapping("hello")
    public Result hello(){
        System.out.println ("Hello World ~");
       // return new Result (1,"success","Hello World");
        return Result.success ("hello world");
    }
    @RequestMapping("/getAddr")
    public Result getAddr(){
        Address addr=new Address ();
        addr.setProvince ( "广东" );
        addr.setCity ( "深圳" );
        return Result.success (addr);
    }
    @RequestMapping("/listAddr")
    public Result listAddr(){
        List
list =new ArrayList<> (); Address addr=new Address (); addr.setProvince ( "广东" ); addr.setCity ( "西安" ); Address addr2=new Address (); addr2.setProvince ( "陕西" ); addr2.setCity ( "西安" ); list.add ( addr ); list.add ( addr2 ); return Result.success (list); } }

HTTP协议,请求响应_第22张图片

六、请求响应---响应案例

HTTP协议,请求响应_第23张图片

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
@RequestMapping("/listEmp")
    public Result list(){
    //1.加载并解析xml文件
    String file=this.getClass ().getClassLoader ().getResource ( "emp.xml" ).getFile ();
    System.out.println ( file );
    List empList = XmlParserUtils.parse ( file, Emp.class );

    //2.对数据进行转换处理
    empList.stream ().forEach (emp -> {
        //处理gender 1:男 2:女
        String gender = emp.getGender ();
        if ("1".equals (gender)){
            emp.setGender ( "男" );
        }else if("2".equals (gender)){
            emp.setGender ( "女" );
        }
        String job = emp.getJob ();
        if ("1".equals ( job )){
            emp.setJob ( "讲师" );
        }else if ("2".equals ( "gender" )){
            emp.setJob ( "班主任" );
        }else  if ("3".equals ( job )){
            emp.setJob ( "就业指导" );
        }
    }  );

    //3.响应数据
    return Result.success (empList);

    }
}

HTTP协议,请求响应_第24张图片

七、分层解耦

HTTP协议,请求响应_第25张图片

HTTP协议,请求响应_第26张图片

HTTP协议,请求响应_第27张图片

HTTP协议,请求响应_第28张图片

你可能感兴趣的:(http,网络协议,网络)