【问题标题】:Why does my Spring Boot POST output a "Whitelabel Error Page"?为什么我的 Spring Boot POST 会输出“Whitelabel Error Page”?
【发布时间】:2020-04-15 15:06:33
【问题描述】:

所以我试图发布某种“登录”窗口,用户必须在其中写下他们的全名才能登录。但我得到了一个“白标签错误页面”。您可以在下面找到我的代码。非常感谢您的帮助

首先我的“用户”类(“vorname”=名字&“nachname”=姓氏):

package POST;

public class User {
    private String vorname;
    private String nachname;

    public User(String vorname, String nachname) {
        this.vorname = vorname;
        this.nachname = nachname;
    }

    public String getVorname() {
        return vorname;
    }

    public void setVorname(String vorname) {
        this.vorname = vorname;
    }

    public String getNachname() {
        return nachname;
    }

    public void setNachname(String nachname) {
        this.nachname = nachname;
    }
}

这是我的“UserData”类:

package POST;

import java.util.List;

public class UserData {

    private List<User> blogs;

    public User createBlog(String vorname, String nachname) {
        User newBlog = new User(vorname, nachname);
        blogs.add(newBlog);
        return newBlog;
    }

    private static UserData instance = null;
    public static UserData getInstance(){
        if(instance == null){
            instance = new UserData();
        }
        return instance;
    }
}

我的“UserAPI”类(“vorname”=名字&“nachname”=姓氏):

package POST;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

public class UserAPI {
    UserData userData = UserData.getInstance();

    @PostMapping(value = "/createUser", consumes = "application/json", produces = "application/json")
    public User createUser(@RequestBody Map<String, String> user) {
        String vorname = user.get("vorname");
        String nachname = user.get("nachname");
        return userData.createBlog(vorname, nachname);
    }
}

还有我的“POSTApplication”类:

package POST;

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

@SpringBootApplication
public class POSTApplication {
    public static void main(String[] args) {

        SpringApplication.run(POSTApplication.class, args);
    }
}

这是我的本地主机上的输出:

白标错误页面 此应用程序没有 /error 的显式映射,因此您看到 这作为后备。 2019 年 12 月 24 日星期二 15:30:49 CET 出现意外错误(类型=未找到,状态=404)。 没有可用的消息

感谢您的帮助。

【问题讨论】:

    标签: java spring spring-boot post


    【解决方案1】:

    404 Http Status Code 表示在服务器中找不到您尝试访问的资源。

    所以,我认为您忘记在 UserAPI 类的顶部添加 @RestController 注释,这就是找不到资源的原因。

    试试这个:

    @RestController
    public class UserAPI {
    UserData userData = UserData.getInstance();
    
    @PostMapping(value = "/createUser", consumes = "application/json", produces = "application/json")
    public User createUser(@RequestBody Map<String, String> user) {
        String vorname = user.get("vorname");
        String nachname = user.get("nachname");
        return userData.createBlog(vorname, nachname);
     }
    }
    

    【讨论】:

    • 嗨,现在我得到一个“状态”:415,“错误”:“不支持的媒体类型”,
    • 从映射中删除消费/生产,这些没有任何作用。并确保您的请求路径正确。 415 表示此映射不存在。它应该是 POST localhost:8080/createUser。
    • 415 表示您发送的数据格式不可接受。我编辑了代码。您可以接受 User 作为 requestbody。
    • 如果你的控制器说它消耗 application/json,那么你的请求需要一个 application/json 的 Content-Type 头 ...
    • 请不要提出更多问题。您最初的问题是关于404 并且已经解决。如有其他问题,请发布一个新问题。在 cmets 上继续使用它们是一种不好的做法。
    猜你喜欢
    • 2019-05-03
    • 2020-01-26
    • 2019-09-10
    • 2019-04-04
    • 2020-07-30
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多