Spring boot接受json指派給java對象
建立
模闆
小書匠
前言
寫這個東西,一方面是我自己在做項目的時候,對json的使用還不是十分的熟悉,對spring boot的使用也不是很熟悉,但是呢,活總是要幹的吧。自己就慢慢的摸索,摸出來了。記錄一下。自己最近也在看《Spring 實戰》,希望早日看完,系統的學習一下spring的知識點
環境
- IDEA
- JDK 1.8
- Spring boot 1.5.8
JSON簡單介紹
之前看了許多的json的教程,但是呢總是覺得看會了,自己卻還是不會用。現在我好像有點了解了這個東西,用我自己的話說:Json是一種資料格式,可以用來表示簡單資料以及複雜資料。
使用Json可以表示以下幾種“東西”:
- 簡單資料
- "hello world"
呐,這就是簡單資料。這個不是重點,是以知道就行了。
2. 對象
簡單的說,使用
{}
大括号括起來的就是對象了,對象有屬性,有值。就像下面這樣:
- {
- "name":"goodboy",
- "sex":"男"
- }
在json這種資料格式中,隻要記住一點: 屬性必須用引号("")括起來
3. 數組
還是簡單的說,數組就是使用
[]
中括号括起來的東西,就像下面這樣
- {
- "name":"goodboy",
- "sex":"男",
- phones:[
- {
- "operator":"中國移動",
- "phoneNum":"159xxxxxxxx"
- },
- {
- "operator":"中國移動",
- "phoneNum":"159xxxxxxxx"
- }
- ]
- }
上述就是這個男的有兩個電話。
Json的簡單介紹就到這裡了。記住兩點,
{}
括起來的是對象,
[]
括起來的是數組。就可以了,其他的在實踐中就會慢慢的了解了。
Spring boot項目的搭建
1、 搭建步驟
這裡使用maven去進行搭建,pom如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>springboot.example</groupId>
- <artifactId>spring-boot-hello</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.8.RELEASE</version>
- </parent>
- <dependencies>
- <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>1.5.8.RELEASE</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.40</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>
- repackage
- </goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
啊,具體的搭建過程,還是自己慢慢去找其他的部落格吧,我這裡就不說了。然後工程的目錄結構如下:

工程目錄結構
這裡需要說明一點的是,Application.java要放在外面,這個外面指的是,controller包的外面,不然掃描不到。當然放裡面也行,出問題也是可以解決的。
2、 代碼
主要就是一個人類Person,這裡面有姓名,性别,住址,以及電話如下所示:
- public class Person {
- private String name;
- private String sex;
- private Address address; // 對象
- private List<Phone> phones; // 數組
- // getter setter
- }
- public class Address {
- private String province;
- private String city;
- private String county;
- private String street;
- // getter setter
- }
- public class Phone {
- private String operator; // 營運商
- private String phoneNum;
- // getter setter
- }
- //Application.java
- @SpringBootApplication
- @RestController
- public class Application {
- @RequestMapping("/")
- String hello(){
- return "hello";
- }
- public static void main(String[] args){
- SpringApplication.run(Application.class,args);
- }
- }
- @RestController
- @RequestMapping("/person")
- public class PersonController {
- @RequestMapping("getPerson")
- public Map<String,Object> getPerson(@RequestBody Person person){
- Map<String,Object> param = new HashMap<String, Object>();
- String s = person.getPhones().toString();
- System.out.println(s);
- param.put("person",person);
- return param;
- }
- }
這裡使用
@RequestBody
, 來接受前端傳輸過來的person對象。
3、 使用postman測試
看,按照格式輸入資料以後,點選send,資料就出來了。去看person,已經由JSON對象變成JAVA對象啦。就可以使用person中的資料做一些想做的事情了。
測試結果
總結
我寫這個主要是為了自己能夠記住這些東西,輸出才是最好的記憶方式,面對的主要還是初學者,各位大佬就不要見笑了。是以重點是什麼呢?
- 在JSON中
括起來的是對象,{}
括起來的是數組[]
- 使用
接受JSON對象,隻要屬性名字與POJO的一緻,那麼資料就會神奇的到了POJO裡面去啦@RequestBody
以上,雖然我是個小菜鳥,但是如果大家有什麼問題,可以留言,我會盡可能幫助大家。希望大神不吝賜教,謝謝。