HTTP-响应协议

package org.example.springbootwebstart;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
@RestController
public class reponseController {



   // :方式一:基于 HttpServletResponse 封装java@RequestMapping("/response")
    @RequestMapping("/response")
    public void response(HttpServletResponse response) throws IOException {
        // 1. 设置响应状态码
        response.setStatus(401);
        // 2. 设置响应头
        response.setHeader("itheima", "itheima");
        // 3. 设置响应体
        response.getWriter().write("

Hello Response

"); } // 方式二:基于 ResponseEntity 封装java@RequestMapping("/response") @RequestMapping("/response1") public ResponseEntity response() { return ResponseEntity.status(401) .header("group", "itcast") .body("

Hello Response

"); // 3. 设置响应体 } }

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