有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring和RESTfull ConcurrentModel不支持空属性值

我正在试验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) 个答案

  1. # 1 楼答案

    您做得不错,但问题可能是由于CryptoData类中的data字段缺少适当的setter/getter。我所说的“适当”是指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