@RequestBody 와 JSon 의 관 계 를 이야기 합 니 다.
5569 단어 @RequestBodyJson
우 리 는@RequestBody 맵 을 통 해
@RequestMapping(value="/queryAccountList.do",produces="application/json;charset=UTF-8")
@ResponseBody
public HashMap<String, Object> queryAccountList(@RequestBody Map<String, Object> paramsMap){
System.out.println("paramsMap="+paramsMap);
String channel= (String) paramsMap.get("channel");
String function_code=(String) paramsMap.get("function_code");
Map<String, Object> reqParam=(Map<String, Object>)paramsMap.get("data");
현재 단 에서 우리 의 인 터 페 이 스 를 호출 할 때 json 문자열 을 전송 하면 map 대상 으로 전 환 됩 니 다.여 기 는 주로@RequestBody 의 밑바닥 실현 입 니 다.우 리 는 토론 하지 않 습 니 다.json 대상 과 json 문자열 의 차이 점:
var person={“name”:”zhangsan”,”sex”:” ”,”age”:”24”}//json
var person='{“name”:”zhangsan”,”sex”:” ”,”age”:”24”}';//json
json 대상 을 json 문자열 로 바 꾸 고 stringify 방법 을 호출 합 니 다.
var person={"name":"zhangsan","sex":" ","age":"24"};//json
var personString = JSON.stringify(person);
alert(personString);
SpringMVC 는 json 문자열 형식 을 받 아들 입 니 다.SpringMVC 에서 REST 를 기반 으로 개발 할 때 프론트 엔 드 가 백 엔 드 로 들 어 오 는 것 은 json 형식의 문자열 이지 json 대상 이 아 닙 니 다.
GET,POST 방식 으로 제시 할 때 request header Content-Type 의 값 에 따라 판단 합 니 다.
application/x-www-form-urlencoded,선택 할 수 있 습 니 다(즉,필요 하지 않 습 니 다.이러한 상황 의 데이터@RequestParam,@ModelAttribute 도 처리 할 수 있 습 니 다.물론@RequestBody 도 처리 할 수 있 습 니 다).
multipart/form-data,처리 할 수 없습니다(즉,@RequestBody 를 사용 하면 이 형식의 데 이 터 를 처리 할 수 없습니다).
기타 형식 은 반드시(기타 형식 은 application/json,application/xml 등 을 포함한다.이 형식의 데 이 터 는@RequestBody 로 처리 해 야 합 니 다).
@RequestBody 처리 유형 과 대상,json 상호 변환
1@RequestBody 처리 유형
프로젝트 에서 controller 에@RequestBody 라 는 글 자 를 자주 볼 수 있 는데 그 는 도대체 어떤 역할 을 합 니까?
일반적으로 폼 으로 데 이 터 를 제출 할 때@RequestBody 를 사용 하지 않 아 도 해당 Bean 에 자동 으로 데 이 터 를 봉인 할 수 있 습 니 다.@RequestBody 는 Content-Type:application/json,application/xml 등 을 처리 합 니 다.
HandlerAdapter 가 설정 한 HttpMessageConverters 를 사용 하여 post data body 를 분석 한 다음 해당 bean 에 연결 합 니 다.
설명:@RequestBody 를 사용 하여 데 이 터 를 분석 하려 면 jackson 이나 fastjson 의존 패 키 지 를 추가 해 야 합 니 다.
maven fastjson 패키지 도입
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
2 대상 과 제 이 슨 의 상호 전환프로젝트 에서 대상 과 json 간 의 상호 전환,공공 류 와 json 대상 전환,정적 내부 류 와 json 대상 전환 을 자주 만 날 수 있 습 니 다.
2.1 내부 클래스 가 없 을 때 Student 클래스
@Data
public class Student {
private String id;
private String name;
private int age;
private String sex;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
json 과 대상 상호 전환
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student();
student.setName("good");
String s = mapper.writeValueAsString(student);
System.out.println(s);
Student hd2 = mapper.readValue(s, Student.class);
System.out.println(hd2);
}
2.2 정적 내부 클래스 가 있 을 때 Student 클래스
@Data
public class Student {
private String id;
private String name;
private int age;
private String sex;
private HomeData homeData;
private BigDecimal salary;
private String[] tel;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Data
public static class HomeData{
private Address address;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Data
public static class Address {
private String country;
private String city;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
}
}
json 과 대상 간 의 상호 전환
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student();
Student.HomeData homeData = new Student.HomeData();
Student.HomeData.Address address = new Student.HomeData.Address();
address.setCountry(" ");
address.setCity(" ");
homeData.setAddress(address);
student.setHomeData(homeData);
String s = mapper.writeValueAsString(address);
System.out.println(s);
Student.HomeData.Address hd2 = mapper.readValue(s, Student.HomeData.Address.class);
System.out.println(hd2);
}
설명:주요 방법 은 mapper.writeValueAsString 과 mapper.readValue 가 있 습 니 다.이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
@RequestBody 와 JSon 의 관 계 를 이야기 합 니 다.springmvc 를 사용 할 때 배경@RequestBody 는 json 형식의 문자열 을 받 아들 입 니 다.문자열 일 것 입 니 다. 현재 단 에서 우리 의 인 터 페 이 스 를 호출 할 때 json 문자열 을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.