gpt4 book ai didi

java - Spring Boot中无法加载资源: the server responded with a status of 404. Whitelabel错误页面

转载 作者:行者123 更新时间:2023-12-01 16:21:46 25 4
gpt4 key购买 nike

I am using spring boot for my application when I run it it show me an error This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jun 06 11:44:29 IST 2020 There was an unexpected error (type=Not Found, status=404). No message available I use my sql for database and sql youg for database editor but it is not able to connect with database

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.material</groupId>
<artifactId>MaterialComp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MaterialComp</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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

</project>

UserEntity.java

package com.material.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="userentity")
public class UserEntity {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;

@Column(name="password")
private String password;

@Column(name="type")
private String type;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}


}

UserRepo.java

package com.material.Repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.material.Entity.UserEntity;

public interface UserRepository extends JpaRepository<UserEntity, Long>{

}

controller.java

package com.material.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.material.Entity.UserEntity;
import com.material.Repo.UserRepository;
@RestController
@CrossOrigin(origins= "http://localhost:4200")
@RequestMapping(path="user")
public class UserController {

@Autowired
private UserRepository userRepo;

@GetMapping("/get")
public List<UserEntity> getUsers(){
return userRepo.findAll();

}
}

application.properties

spring.datasource.url=jdbc:mysql://localhost/ecommerce
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.use-new-id-generator-mappings=false

最佳答案

这里有一些棘手的事情。

首先,您已将服务器配置为监听 localhost:4200,这很好。您已经用这一行完成了这一点:

@CrossOrigin(origins= "http://localhost:4200")

接下来你告诉了 spring:

@RequestMapping(path="user")

Spring 将获取 user 并将其添加到 http://localhost:4200。所以它现在正在监听 http://localhost:4200user - 这不是您想要的,请尝试添加 / 以便您的请求映射如下所示:

@RequestMapping(path="/user")

您的应用程序现在应该正在监听 http://localhost:4200/user 。太棒了!

接下来,要调用 getUsers 方法,您已经定义了 @GetMapping("/get"),它与其他注释结合起来意味着要调用 getUsers,您需要进行 GET 操作http://localhost:4200/user/get,假设一切正常。

一个小技巧是,@GetMapping 实际上是 @RequestMapping(method = RequestMethod.GET) 的别名 - 因此要整理它,您可以删除 @RequestMapping(path="user") 并使用

@GetMapping(value = "/users") 这意味着针对 http://localhost:4200/userGET 将激活您的 getUsers 方法。

关于java - Spring Boot中无法加载资源: the server responded with a status of 404. Whitelabel错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62254065/

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