我正在使用Hibernate和MySQL构建此Spring Boot应用程序。我知道这是非常基本的,并且多次询问相同的问题,但是我无法弄清楚为什么控制器没有命中并给出404错误。如我所见,问题出在ComponentScan
,其中@SpringBootApplication
和@RestController
驻留在一个程序包中,而@Repository
和@Entity
则位于另一个程序包中。当我将软件包包含为@ComponentScan(basePackages = "com.sample.user")
时,项目将成功构建并运行,但未命中GET方法getUser()
,也没有控制台输出错误。 GET方法仅在我省略控制器类中的@Autowired private UserRepository userRepository;
以及应用程序类中的@ComponentScan
。
Controller
package com.sample.rest.controller;
import com.sample.user.entity.User;
import com.sample.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping ("user")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public User getUser() {
User user = new User();
user.setFirstName("Lady");
user.setLastName("Gaga");
user.setEmail("[email protected]");
userRepository.save(user);
return user;
}
}
应用程序
package com.sample.rest;
import com.sample.rest.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.sample.user")
public class RestServicesApplication {
public static void main(String[] args) {
SpringApplication.run(RestServicesApplication.class, args);
}
}
存储库接口
package com.sample.user.repository;
import com.sample.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
@EnableJpaRepositories
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
实体
package com.sample.user.entity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email_address", nullable = false)
private String email;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
使用@ComponentScan(basePackages = "com.sample.user")
,您正在覆盖默认行为。
因此,请删除此包装并将包装放在您有@SpringBootApplication
的包装下面,或将所有包装添加到@ComponentScan
。
我建议不要使用默认的Spring Boot行为。
似乎您的请求映射在控制器级别设置不正确。插入您现在拥有的@RequestMapping ("user")
,请将其设置为@RequestMapping ("/user")
现在请求GET“ {HOST} / user /”-不要忘了最后的/,因为您已经在getUser()
端点的方法级别定义了该名称>