天天看點

Spring boot接受json指派給java對象

Spring boot接受json指派給java對象

建立

模闆

小書匠

前言

寫這個東西,一方面是我自己在做項目的時候,對json的使用還不是十分的熟悉,對spring boot的使用也不是很熟悉,但是呢,活總是要幹的吧。自己就慢慢的摸索,摸出來了。記錄一下。自己最近也在看《Spring 實戰》,希望早日看完,系統的學習一下spring的知識點

環境

  • IDEA
  • JDK 1.8
  • Spring boot 1.5.8

JSON簡單介紹

之前看了許多的json的教程,但是呢總是覺得看會了,自己卻還是不會用。現在我好像有點了解了這個東西,用我自己的話說:Json是一種資料格式,可以用來表示簡單資料以及複雜資料。

使用Json可以表示以下幾種“東西”:

  1. 簡單資料
  1. "hello world" 

呐,這就是簡單資料。這個不是重點,是以知道就行了。

2. 對象

簡單的說,使用

{}

大括号括起來的就是對象了,對象有屬性,有值。就像下面這樣:

  1. "name":"goodboy", 
  2. "sex":"男" 

在json這種資料格式中,隻要記住一點: 屬性必須用引号("")括起來

3. 數組

還是簡單的說,數組就是使用

[]

中括号括起來的東西,就像下面這樣

  1. "name":"goodboy", 
  2. "sex":"男", 
  3. phones:[ 
  4. "operator":"中國移動", 
  5. "phoneNum":"159xxxxxxxx" 
  6. },  
  7. "operator":"中國移動", 
  8. "phoneNum":"159xxxxxxxx" 

上述就是這個男的有兩個電話。

Json的簡單介紹就到這裡了。記住兩點,

{}

括起來的是對象,

[]

括起來的是數組。就可以了,其他的在實踐中就會慢慢的了解了。

Spring boot項目的搭建

1、 搭建步驟

這裡使用maven去進行搭建,pom如下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" 
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  5. <modelVersion>4.0.0</modelVersion> 
  6. <groupId>springboot.example</groupId> 
  7. <artifactId>spring-boot-hello</artifactId> 
  8. <version>1.0-SNAPSHOT</version> 
  9. <packaging>jar</packaging> 
  10. <parent> 
  11. <groupId>org.springframework.boot</groupId> 
  12. <artifactId>spring-boot-starter-parent</artifactId> 
  13. <version>1.5.8.RELEASE</version> 
  14. </parent> 
  15. <dependencies> 
  16. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> 
  17. <dependency> 
  18. <groupId>org.springframework.boot</groupId> 
  19. <artifactId>spring-boot-starter-web</artifactId> 
  20. <version>1.5.8.RELEASE</version> 
  21. </dependency> 
  22. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 
  23. <dependency> 
  24. <groupId>com.alibaba</groupId> 
  25. <artifactId>fastjson</artifactId> 
  26. <version>1.2.40</version> 
  27. </dependency> 
  28. </dependencies> 
  29. <build> 
  30. <plugins> 
  31. <plugin> 
  32. <groupId>org.springframework.boot</groupId> 
  33. <artifactId>spring-boot-maven-plugin</artifactId> 
  34. <executions> 
  35. <execution> 
  36. <goals> 
  37. <goal> 
  38. repackage 
  39. </goal> 
  40. </goals> 
  41. </execution> 
  42. </executions> 
  43. </plugin> 
  44. </plugins> 
  45. </build> 
  46. </project> 

啊,具體的搭建過程,還是自己慢慢去找其他的部落格吧,我這裡就不說了。然後工程的目錄結構如下:

Spring boot接受json指派給java對象

工程目錄結構

這裡需要說明一點的是,Application.java要放在外面,這個外面指的是,controller包的外面,不然掃描不到。當然放裡面也行,出問題也是可以解決的。

2、 代碼

主要就是一個人類Person,這裡面有姓名,性别,住址,以及電話如下所示:

  1. public class Person { 
  2. private String name; 
  3. private String sex; 
  4. private Address address; // 對象 
  5. private List<Phone> phones; // 數組 
  6. // getter setter  
  7. public class Address { 
  8. private String province; 
  9. private String city; 
  10. private String county; 
  11. private String street; 
  12. // getter setter  
  13. public class Phone { 
  14. private String operator; // 營運商 
  15. private String phoneNum; 
  16. // getter setter  
  1. //Application.java 
  2. @SpringBootApplication 
  3. @RestController 
  4. public class Application { 
  5. @RequestMapping("/") 
  6. String hello(){ 
  7. return "hello"; 
  8. public static void main(String[] args){ 
  9. SpringApplication.run(Application.class,args); 
  1. @RestController 
  2. @RequestMapping("/person") 
  3. public class PersonController { 
  4. @RequestMapping("getPerson") 
  5. public Map<String,Object> getPerson(@RequestBody Person person){ 
  6. Map<String,Object> param = new HashMap<String, Object>(); 
  7. String s = person.getPhones().toString(); 
  8. System.out.println(s); 
  9. param.put("person",person); 
  10. return param; 

這裡使用

@RequestBody

, 來接受前端傳輸過來的person對象。

3、 使用postman測試

看,按照格式輸入資料以後,點選send,資料就出來了。去看person,已經由JSON對象變成JAVA對象啦。就可以使用person中的資料做一些想做的事情了。

Spring boot接受json指派給java對象

測試結果

總結

我寫這個主要是為了自己能夠記住這些東西,輸出才是最好的記憶方式,面對的主要還是初學者,各位大佬就不要見笑了。是以重點是什麼呢?

  • 在JSON中

    {}

    括起來的是對象,

    []

    括起來的是數組
  • 使用

    @RequestBody

    接受JSON對象,隻要屬性名字與POJO的一緻,那麼資料就會神奇的到了POJO裡面去啦

以上,雖然我是個小菜鳥,但是如果大家有什麼問題,可以留言,我會盡可能幫助大家。希望大神不吝賜教,謝謝。