REST GET请求未将List <object>显示为JSON

时间:2019-03-02 11:26:56

标签: json rest spring-boot java-8 get

我是Spingboot的新手,正在做一个非常简单的应用程序的演示(示例),以JSON形式显示类列表,但出现错误。我在配置/依赖项中错过了什么?

我的pom.xml:

<?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>
     <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.1.3.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>com.beniregev</groupId>
     <artifactId>booking-demo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>booking-demo</name>
     <description>Demo project for Spring Boot</description>

     <properties>
          <java.version>1.8</java.version>
     </properties>

     <dependencies>
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
          </dependency>

          <dependency>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <scope>runtime</scope>
          </dependency>
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
          </dependency>
     </dependencies>

     <build>
          <plugins>
                <plugin>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
          </plugins>
     </build>

 </project>

我的目录结构: Application Directories structure

DemoController.java-这正在工作(“ localhost:8080 / hello”)

package com.beniregev.bookingdemo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello";
    }
}

HotelBooking.java

package com.beniregev.bookingdemo;

public class HotelBooking {
    private String hotelName;
    private double pricePerNight;
    private int numberOfNights;

    public HotelBooking(String hotelName, double pricePerNight, int numberOfNights) {
        this.hotelName = hotelName;
        this.pricePerNight = pricePerNight;
        this.numberOfNights = numberOfNights;
    }

    public String getHotelName() {
        return hotelName;
    }

    public double getPricePerNight() {
        return pricePerNight;
    }

    public int getNumberOfNights() {
        return numberOfNights;
    }

    public double getTotalPrica() {
        return pricePerNight * numberOfNights;
    }
}

BookingController.java

package com.beniregev.bookingdemo;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value="/bookings")
public class BookingController {
    private List<HotelBooking> bookings;

    public BookingController() {
        bookings = new ArrayList<>();

        bookings.add(new HotelBooking("Marriot", 200.50, 3));
        bookings.add(new HotelBooking("Novotel", 140.74, 1));
        bookings.add(new HotelBooking("Ibis", 90.0, 4));
        bookings.add(new HotelBooking("Hilton", 150.60, 5));
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<HotelBooking> getAll() {
        return bookings;
    }
}

BookingDemoApplication.java

package com.beniregev.bookingdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookingDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BookingDemoApplication.class, args);
    }

}

我正在使用IntelliJ 2018.3,JDK 1.8.0_144和Springboot。 当我运行服务器并转到“ localhost:8080 / bookings / all”而不是JSON时,得到以下信息: JSON is not displayed

为什么列表未显示为JSON?我想念什么?

我希望收到类似以下内容的信息: Expected result- JSON

我将不胜感激。

2 个答案:

答案 0 :(得分:1)

您的BookingController#getAll()方法返回List,它将转换为JSON有效负载。在客户端,您会看到它是JSON。在JSONObject页中,它不会神奇地显示为HTML变量。为此,您需要template engine。您还可以使用静态index.html文件在客户端玩API。从此开始要容易得多。

要执行此操作,您需要添加resources/static文件夹index.html文件,其内容如下:

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hotel bookings</title>
</head>
<body>
<h1>Hotel bookings</h1>
<table class="table">
    <thead>
    <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Number of nights</th>
        <th scope="col">Price per night</th>
        <th scope="col">Total price</th>
    </tr>
    </thead>
    <tbody class="bookingsBody"></tbody>
</table>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    function laodAllHotelBookings() {
        var url = '/bookings/all/';
        var headers = {
            headers: {
                'Content-Type': 'application/json'
            }
        };
        axios.get(url, headers).then(res => {
            let bookingsBody = $('.bookingsBody');
            let data = res.data;
            $.each(Object.keys(data), function (index, key) {
                let item = data[key];
                var row = $('<tr>');
                row.append($('<th>').text(index));
                row.append($('<td>').text(item.hotelName));
                row.append($('<td>').text(item.pricePerNight));
                row.append($('<td>').text(item.numberOfNights));
                row.append($('<td>').text(item.totalPrice));

                bookingsBody.append(row);
            })
        }).catch(error => {
            console.log("error", error);
            const errorMsg = 'There was an error fetching the menu';
            console.log(errorMsg);
        });
    }

    laodAllHotelBookings();
</script>
</body>
</html>

要向application.properties文件添加以下行:

spring.resources.static-locations[0]=file:src/main/resources/static/

足以看到下面http://localhost:8080/下的内容: enter image description here

在使用pricesmoney中的Java时,通常要使用BigDecimal。我已经修复了您的POJO

import java.math.BigDecimal;

public class HotelBooking {
    private String hotelName;
    private BigDecimal pricePerNight;
    private int numberOfNights;

    public HotelBooking(String hotelName, BigDecimal pricePerNight, int numberOfNights) {
        this.hotelName = hotelName;
        this.pricePerNight = pricePerNight;
        this.numberOfNights = numberOfNights;
    }

    public String getHotelName() {
        return hotelName;
    }

    public BigDecimal getPricePerNight() {
        return pricePerNight;
    }

    public int getNumberOfNights() {
        return numberOfNights;
    }

    public BigDecimal getTotalPrice() {
        return pricePerNight.multiply(BigDecimal.valueOf(numberOfNights));
    }
}

您的控制器构造函数如下所示:

public BookingController() {
    bookings = new ArrayList<>();

    bookings.add(new HotelBooking("Marriot", new BigDecimal("200.50"), 3));
    bookings.add(new HotelBooking("Novotel", new BigDecimal("140.74"), 1));
    bookings.add(new HotelBooking("Ibis", new BigDecimal("90.0"), 4));
    bookings.add(new HotelBooking("Hilton", new BigDecimal("150.60"), 5));
}

在示例inex.html中,我使用了axiosbootstrap库。

答案 1 :(得分:0)

HotelBooking必须是JavaBean。为了使其成为一体,它必须:

  1. 具有没有参数的构造函数
  2. 对所有属性都具有吸气剂和塞特剂

因此,如果您将班级更改为此:

package com.beniregev.bookingdemo;

public class HotelBooking {
    private String hotelName;
    private double pricePerNight;
    private int numberOfNights;

    public HoterBooking() 
    {
    }

    public HotelBooking(String hotelName, double pricePerNight, int numberOfNights) {
        this.hotelName = hotelName;
        this.pricePerNight = pricePerNight;
        this.numberOfNights = numberOfNights;
    }

    public String getHotelName() {
        return hotelName;
    }

    public void setHotelName(String val) {
        this.hotelName = val;
    }

    public double getPricePerNight() {
        return pricePerNight;
    }

    public void setPricePerNight(double val) {
        this.pricePerNight = val;
    }

    public int getNumberOfNights() {
        return numberOfNights;
    }

    public void setNumberOfNights(int val) {
        this.numberOfNights = val;
    }

    public double getTotalPrica() {
        return pricePerNight * numberOfNights;
    }
}

它应该可以正常工作。

顺便说一句。 请勿使用double类型输入货币值。请改用java.math.BigDecimal