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如下:
- "1.0" encoding="UTF-8"
xml version=
- <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
-
- public class Application {
-
- "/")
- String hello(){
- return "hello";
- }
-
- public static void main(String[] args){
- SpringApplication.run(Application.class,args);
- }
-
- }
- "/person")
- public class PersonController {
- "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中
{}
括起來的是對象,[]
括起來的是數組 - 使用
@RequestBody
接受JSON對象,只要屬性名字與POJO的一致,那么數據就會神奇的到了POJO里面去啦
以上,雖然我是個小菜鳥,但是如果大家有什么問題,可以留言,我會盡可能幫助大家。希望大神不吝賜教,謝謝。