>  기사  >  Java  >  Java는 Spring이 웹 요청 매개변수를 수신하는 방법을 자세히 설명합니다.

Java는 Spring이 웹 요청 매개변수를 수신하는 방법을 자세히 설명합니다.

青灯夜游
青灯夜游앞으로
2018-10-25 16:39:042218검색

이 기사에서는 Spring이 Java에서 웹 요청 매개변수를 수신하는 방법에 대한 자세한 설명을 제공합니다. 도움이 필요한 친구들이 참고할 수 있기를 바랍니다.

1 쿼리 매개변수

요청 형식: url?parameter1=value1¶meter2=value2 ...#🎜 🎜#GET 및 POST 메서드 모두에 적용 가능
Spring에서 쿼리 매개변수를 처리하는 방법에는 여러 가지가 있습니다.

방법 1:

메서드 매개변수 이름은 요청 매개변수 이름입니다#🎜🎜 #

  // 查询参数1
  @RequestMapping(value = "/test/query1", method = RequestMethod.GET)  
  public String testQuery1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

방법 2: HttpServletRequest에서 매개변수 추출

  // 查询参数2
  @RequestMapping(value = "/test/query2", method = RequestMethod.GET)  
  public String testQuery2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

방법 3: 메서드 매개변수 이름과 요청 매개변수 이름은 바인딩 매개변수를 통해 다를 수 있습니다. @RequestParam 주석

  // 查询参数3
  @RequestMapping(value = "/test/query3", method = RequestMethod.GET)  
  public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);    
    return "username=" + un + ", password=" + pw;
  }

방법 4: 매개변수 캐리어로 엔터티 클래스 개체를 생성합니다. Spring은 매개변수 이름을 기반으로 매개변수를 엔터티 클래스 개체의 속성에 자동으로 바인딩합니다. 🎜🎜#
  // 查询参数4
  @RequestMapping(value = "/test/query4", method = RequestMethod.GET)  
  public String testQuery4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

엔티티 클래스는 다음과 같이 정의됩니다.

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builderpublic class User {  
   private String username;  
   private String password;
}
여기에서는 타사 라이브러리 lombok이 사용되므로 get, set 등의 메서드를 수동으로 추가할 필요가 없습니다. 코드에서 lombok은 자동으로 추가됩니다.

요청을 보내는 컬 명령은 다음과 같습니다.

curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'
대화 메시지는 다음과 같습니다.

GET /test/query1?username=aaa&password=bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:01:30 GMT

username=aaa, password=bbb

#🎜 🎜#2 양식 매개변수# 🎜🎜#

요청 매개변수는 URL이 아니라 본문에 있습니다. 형식은 url?매개변수 1=값 1&매개변수 2=값 2입니다. ... POST 방식에 적합

양식 매개변수 처리 방식은 메소드 방식을 RequestMethod 주석의 POST 메서드

방법 1:

  // 表单参数1
  @RequestMapping(value = "/test/form1", method = RequestMethod.POST)  
  public String testForm1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
방법 2:

  // 表单参数2
  @RequestMapping(value = "/test/form2", method = RequestMethod.POST)  
  public String testForm2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
방법 3:

  // 表单参数3
  @RequestMapping(value = "/test/form3", method = RequestMethod.POST)  
  public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);    
    return "username=" + un + ", password=" + pw;
  }
방법 4:

  // 表单参数4
  @RequestMapping(value = "/test/form4", method = RequestMethod.POST)
  public String testForm4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
컬 요청 명령은 다음과 같습니다:

curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1
요청 및 응답 메시지는 다음과 같습니다.

POST /test/form1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

username=aaa&password=bbbHTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:05:35 GMT

username=aaa, password=bbb
#🎜🎜 #

3 경로 매개변수

#🎜🎜 #

요청 매개변수는 URL의 일부이며 형식은 url/매개변수 1/매개변수 2...#🎜🎜 #GET, POST 방식 모두 적용 가능

코드는 다음과 같습니다.

  @RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)  
  public String testUrl(@PathVariable String username, @PathVariable String password) {
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
# 🎜🎜#요청 컬 명령은 다음과 같습니다.
curl -i http://192.168.1.14:8080/test/url/aaa/bbb
요청 및 응답 메시지는 다음과 같습니다. 다음과 같습니다:

GET /test/url/aaa/bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:07:44 GMT

username=aaa, password=bbb


4 json 형식 매개변수
# 🎜🎜#

요청 매개변수는 본문에 있으며 JSON 형식. 요청 헤더를 추가해야 합니다: Content-Type: application/json;charset=UTF-8

POST 메서드에 적용 가능

방법 1:

엔티티 클래스를 정의하고 json 개체를 강도 클래스로 구문 분석해야 합니다. RequestBody 주석 추가

  // json参数1
  @RequestMapping(value = "/test/json1", method = RequestMethod.POST)  
  public String testJson1(@RequestBody User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
방법 2: 엔티티 클래스를 정의하지 않으려면 json 요청을 JSONObject 클래스로 직접 구문 분석할 수도 있습니다#🎜🎜 #
  // json参数2
  @RequestMapping(value = "/test/json2", method = RequestMethod.POST)  
  public String testJson2(@RequestBody JSONObject json) {
    String username = json.getString("username");
    String password = json.getString("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
방법 3:

json 객체를 Map 객체로 직접 구문 분석할 수도 있습니다.

  // json参数3
  @RequestMapping(value = "/test/json3", method = RequestMethod.POST)  
  public String testJson3(@RequestBody Map18fed3eaa375b9cabd6da695ec776cac userMap) {
    String username = userMap.get("username");
    String password = userMap.get("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }

요청 컬 명령은 다음과 같습니다.
curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '{    
   "username" : "aaa",    
   "password" : "bbb"
} 
'http://192.168.1.14:8080/test/json1
요청 및 응답 메시지는 다음과 같습니다.

POST /test/json1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json;charset=UTF-8
Content-Length: 52


{
    "username" : "aaa",
    "password" : "bbb"
}HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:09:06 GMT

username=aaa, password=bbb

위 내용은 Java는 Spring이 웹 요청 매개변수를 수신하는 방법을 자세히 설명합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제