java获取请求参数案例代码和设置响应数据

[复制链接]
admin 发表于 2025-8-21 10:16:05 | 显示全部楼层 |阅读模式
java获取请求参数案例代码

  1. package com.jinhei;

  2. import jakarta.servlet.http.HttpServletRequest;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;

  5. @RestController
  6. public class RequestController {
  7.     /**
  8.      * 请求路径 http://localhost:8080/request?name=Tom&age=18
  9.      * @param request
  10.      * @return
  11.      */
  12.     @RequestMapping("/request")
  13.     public String request(HttpServletRequest request){
  14.         //1.获取请求参数 name, age
  15.         String name = request.getParameter("name");
  16.         String age = request.getParameter("age");
  17.         System.out.println("name = " + name + ", age = " + age);

  18.         //2.获取请求路径
  19.         String uri = request.getRequestURI();
  20.         String url = request.getRequestURL().toString();
  21.         System.out.println("uri = " + uri);
  22.         System.out.println("url = " + url);

  23.         //3.获取请求方式
  24.         String method = request.getMethod();
  25.         System.out.println("method = " + method);

  26.         //4.获取请求头
  27.         String header = request.getHeader("User-Agent");
  28.         System.out.println("header = " + header);
  29.         return "request success";
  30.     }
  31. }
复制代码
输出结果如下:

name = Tom, age = 18
uri = /request
url = http://localhost:8080/request
method = GET
header = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36


  1. package com.jinhei;

  2. import jakarta.servlet.http.HttpServletResponse;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;

  6. import java.io.IOException;

  7. @RestController
  8. public class ResponseController {

  9.     @RequestMapping("/response")
  10.     public void response(HttpServletResponse response) throws IOException {
  11.         //1.设置响应状态码
  12.         response.setStatus(401);
  13.         //2.设置响应头
  14.         response.setHeader("name","itcast");
  15.         //3.设置响应体
  16.         response.setContentType("text/html;charset=utf-8");
  17.         response.setCharacterEncoding("utf-8");
  18.         response.getWriter().write("<h1>hello response</h1>");
  19.     }

  20.     @RequestMapping("/response2")
  21.     public ResponseEntity<String> response2(HttpServletResponse response) throws IOException {
  22.         return ResponseEntity
  23.                 .status(401)
  24.                 .header("name","itcast")
  25.                 .body("<h1>hello response</h1>");
  26.     }

  27. }
复制代码

/**
* 删除部门 - 方式二: @RequestParam
* 注意事项: 一旦声明了@RequestParam, 该参数在请求时必须传递, 如果不传递将会报错. (默认 required true)
*/
/*@DeleteMapping("/depts")
public Result delete(@RequestParam(value = "id", required = false) Integer deptId){
    System.out.println("根据ID删除部门: " + deptId);
    return Result.success();
}*/

@RequestParam注解required属性默认为true,代表该参数必须传递,如果不传递将报错。 如果参数可选,可以将属性设置为false。

/**
* 删除部门 - 方式三: 省略@RequestParam (前端传递的请求参数名与服务端方法形参名一致) [推荐]
*  delete htt p://localhost:8080/depts?id=1
*/
@DeleteMapping
public Result delete(Integer id){
    //System.out.println("根据ID删除部门: " + id);
log.info("根据ID删除部门: {}", id);
    deptService.deleteById(id);
    return Result.success();
}


网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表