为什么Spring后端会不断给我不兼容类型的whitelabel错误?

时间:2019-06-22 04:06:37

标签: java thymeleaf

我有一个带有嵌入式百里香模板的springboot后端。但是后端一直给我白色标签错误页面:400错误的请求, 无法将类型“ java.lang.String”的值转换为所需的类型“ java.lang.Long”;嵌套的异常是java.lang.NumberFormatException:对于输入字符串:“列表”。我想知道为什么吗?

我尝试将用户的ID从Long更改为Double,但是它不能解决任何问题,我的后端适用于其他路线,例如:当我访问/ hello时,它会打印“ hello world”,这是我的另一条路线已经写过。

// this is my user controller class
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping
    public ModelAndView list(Model model) {
        model.addAttribute("userList", userRepository.listUsers());
        model.addAttribute("title", "user management");
        return new ModelAndView("users/list", "userModel", model);
    }


// this is my repository implementation class
@Repository
public class UserRepositoryImpl implements UserRepository {
    private static final AtomicLong counter = new AtomicLong();
    private final ConcurrentMap<Long, User> userMap = new ConcurrentHashMap<>();

    @Override
    public User saveOrUpdateUser(User user) {
        Long id = user.getId();
        if(id == null) {
            id = counter.incrementAndGet();
            user.setId(id);
        }
        this.userMap.put(id, user);
        return user;
    }

    @Override
    public void deleteUser(Long id) {
        this.userMap.remove(id);

    }

    @Override
    public User getUserById(Long id) {
        // TODO Auto-generated method stub
        return this.userMap.get(id);
    }

    @Override
    public List<User> listUsers() {
        // TODO Auto-generated method stub
        return new ArrayList<User>(this.userMap.values());
    }

}

// the page I wish to render
<!DOCTYPE html>
<html   xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
    <head>
        <meta charset="UTF-8"></meta>
        <title>Thymeleaf in action</title>
    </head>
    <body>
        <div th:replace="~{fragments/header :: header}"></div>
        <div th:replace="~{fragments/header :: footer}"></div>
    </body>
</html>

// the user object class
package com.waylau.spring.boot.blog.domain;

public class User {
    private Long id;
    private String name;
    private String email;

    public User() {

    }

    public User(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

// my build class with gradle
buildscript {
    repositories {
        //mavenCentral()

    }

    //ext['thymeleaf.version'] = '3.0.3.RELEASE'
    //ext['thymeleaf-layout-dialect.version'] = '2.2.0'

    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.21.RELEASE'
    }
}

plugins {
    id 'java'
}

apply plugin: 'org.springframework.boot'

group = 'com.waylau.spring.boot.blog'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    testCompile 'org.springframework.boot:spring-boot-starter-test'

}

0 个答案:

没有答案