Java는 Spring이 웹 요청 매개변수를 수신하는 방법을 자세히 설명합니다.
이 기사에서는 Spring이 Java에서 웹 요청 매개변수를 수신하는 방법에 대한 자세한 설명을 제공합니다. 도움이 필요한 친구들이 참고할 수 있기를 바랍니다.
1 쿼리 매개변수
요청 형식: url?parameter1=value1¶meter2=value2 ...#🎜 🎜#GET 및 POST 메서드 모두에 적용 가능
Spring에서 쿼리 매개변수를 처리하는 방법에는 여러 가지가 있습니다.
메서드 매개변수 이름은 요청 매개변수 이름입니다#🎜🎜 #
// 查询参数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; }
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 @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 @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 @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; }
// 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; }
json 객체를 Map 객체로 직접 구문 분석할 수도 있습니다.
// json参数3 @RequestMapping(value = "/test/json3", method = RequestMethod.POST) public String testJson3(@RequestBody Map<String, String> 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Java의 난수 생성기 안내. 여기서는 예제를 통해 Java의 함수와 예제를 통해 두 가지 다른 생성기에 대해 설명합니다.

Java의 Weka 가이드. 여기에서는 소개, weka java 사용 방법, 플랫폼 유형 및 장점을 예제와 함께 설명합니다.

자바의 암스트롱 번호 안내 여기에서는 일부 코드와 함께 Java의 Armstrong 번호에 대한 소개를 논의합니다.

Java의 Smith Number 가이드. 여기서는 정의, Java에서 스미스 번호를 확인하는 방법에 대해 논의합니다. 코드 구현의 예.

이 기사에서는 가장 많이 묻는 Java Spring 면접 질문과 자세한 답변을 보관했습니다. 그래야 면접에 합격할 수 있습니다.

Java 8은 스트림 API를 소개하여 데이터 컬렉션을 처리하는 강력하고 표현적인 방법을 제공합니다. 그러나 스트림을 사용할 때 일반적인 질문은 다음과 같은 것입니다. 기존 루프는 조기 중단 또는 반환을 허용하지만 스트림의 Foreach 메소드는이 방법을 직접 지원하지 않습니다. 이 기사는 이유를 설명하고 스트림 처리 시스템에서 조기 종료를 구현하기위한 대체 방법을 탐색합니다. 추가 읽기 : Java Stream API 개선 스트림 foreach를 이해하십시오 Foreach 메소드는 스트림의 각 요소에서 하나의 작업을 수행하는 터미널 작동입니다. 디자인 의도입니다
