gpt4 book ai didi

java - 为什么我的 Spring Boot POST 输出 "Whitelabel Error Page"?

转载 作者:行者123 更新时间:2023-12-05 00:13:32 25 4
gpt4 key购买 nike

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

首先是我的“用户”类(“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);
}
}

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

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Dec 24 15:30:49 CET 2019 There was an unexpected error (type=Not Found, status=404). No message available

感谢您的帮助。

最佳答案

404 Http 状态代码 表示您尝试访问的资源未在服务器中找到。

所以,我认为您忘记在 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);
}
}

关于java - 为什么我的 Spring Boot POST 输出 "Whitelabel Error Page"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59470350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com