HTTP协议,请求响应

这篇具有很好参考价值的文章主要介绍了HTTP协议,请求响应。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

、概述

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

二、HTTP请求协议

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

三、HTTP响应协议

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

四、请求数据

1.简单实体参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

2.实体参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

3.数组参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

4.集合参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

    }

5.日期参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

6.Json参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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

7.路径参数

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

    //路径参数
    @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协议,请求响应,JAVAWeb,http,网络协议,网络

五、响应数据

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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<Address> listAddr(){
        List<Address> 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协议,请求响应,JAVAWeb,http,网络协议,网络

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<Address> 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协议,请求响应,JAVAWeb,http,网络协议,网络

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

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

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<Emp> 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协议,请求响应,JAVAWeb,http,网络协议,网络

七、三层架构

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

八、分层解耦

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

九、IOC&DI案例

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

 

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

十、IOC详解

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

十一、DI详解

1.primary 为bean 设置优先级

2.Quakifier 指定注入的bean

3.Resource 用名字注入     AutoWired默认按照类型注入

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络

HTTP协议,请求响应,JAVAWeb,http,网络协议,网络文章来源地址https://www.toymoban.com/news/detail-729056.html

到了这里,关于HTTP协议,请求响应的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包