Spring和RESTfull ConcurrentModel不支持空属性值

时间:2018-10-19 09:42:22

标签: java json spring rest illegalargumentexception

我正在尝试Spring和Rest,以便构建和Web应用程序来表示简单的加密货币列表。

控制器:

package jasmin.merusic.cryptocurrency.controllers;

import jasmin.merusic.cryptocurrency.services.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataController {

 private final ApiService apiService;

public DataController(ApiService apiService) {
    this.apiService = apiService;
}

@RequestMapping({"", "/", "/index","cryptos"})
public String index(Model model){

    model.addAttribute("cryptos",apiService.getCrypto(10));

    return "index";
}
}

然后我有了服务包,这里有Api的接口和该服务的实现(在这里我重写了方法):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

 import java.util.List;

@Service
public class ApiServiceImpl implements ApiService{

private   RestTemplate restTemplate;

@Autowired
public ApiServiceImpl(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
}

@Override
public List<Crypto> getCrypto(Integer limit) {

    CryptoData crypto = restTemplate.getForObject("https://api.coinmarketcap.com/v2/ticker/?limit=" + limit , CryptoData.class);


    return crypto.getCryptos();
}
}

这是RestTamplateConfig类的代码

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){

    return builder.build();
}
}

这是我试图从API映射的类:`

public class CryptoData {
private List<Crypto> data;

public List<Crypto> getCryptos() {
    return data;
}

public void setCryptos(List<Crypto> data) {
    this.data = data;
}
}

public class Crypto implements Serializable
{

private Integer id;
private String name;
private String symbol;
private String websiteSlug;
private Integer rank;
private Double circulatingSupply;
private Double totalSupply;
private Double maxSupply;
private Quotes quotes;
private Integer lastUpdated;
private final static long serialVersionUID = 362556439034076810L;
//getters and setter

` 我的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>jasmin.merusic</groupId>
<artifactId>cryptocurrency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cryptocurrency</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

问题是我无法将数据绑定到POJO,并且出现错误

2018-10-19 10:39:35.251 ERROR 14188 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/index]

java.lang.IllegalArgumentException: ConcurrentModel does not support null attribute value
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:75) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:39) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at jasmin.merusic.cryptocurrency.controllers.DataController.index(DataController.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

有什么帮助吗?我对此很陌生,为了这个我一直在关注一些事情。

1 个答案:

答案 0 :(得分:1)

您做得对,但问题可能是由于data类中的CryptoData字段缺少适当的设置方法/获取方法。 “适当”是指getData()setData(List<Crypto>)

如果您仔细查看收到的JSON代码段,则会发现其结构与您的结构有些不同。实际上是Map<Integer, Crypto>,而不是List<Crypto>

class CryptoData {
    private Map<Integer, Crypto> data;

    public Map<Integer, Crypto> getData() {
        return data;
    }

    public void setData(Map<Integer, Crypto> data) {
        this.data = data;
    }
}

enter image description here