简体   繁体   English

Spring 启动 controller 未能命中错误 404

[英]Spring boot controller fails to hit error 404

I am building this Spring Boot application with Hibernate and MySQL.我正在使用 Hibernate 和 MySQL 构建这个 Spring 引导应用程序。 I know this is pretty basic and the same question is asked multiple times but I can't figure out why my controller isn't hitting and gives 404 error.我知道这是非常基本的,并且多次询问相同的问题,但我无法弄清楚为什么我的 controller 没有命中并给出 404 错误。 As I see, the problem is with ComponentScan where my @SpringBootApplication and @RestController reside within one package while my @Repository and @Entity lie in another package.如我所见,问题在于ComponentScan ,其中我的@SpringBootApplication@RestController位于一个 package 中,而我的@Repository@Entity位于另一个 package 中。 When I include the package as @ComponentScan(basePackages = "com.sample.user") , the project builds and runs successfully but does not hit the GET method getUser() and no console output of an error as well.当我将 package 包含为@ComponentScan(basePackages = "com.sample.user")时,项目构建并成功运行,但没有命中 GET 方法getUser()并且没有控制台 output 也出现错误。 The GET method hits only when I omit the @Autowired private UserRepository userRepository; GET 方法仅在我省略@Autowired private UserRepository userRepository; from the controller class along with @ComponentScan in application class.来自 controller class 以及应用程序 class 中的@ComponentScan

Controller 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("l.gaga@ymail.com");
        userRepository.save(user);
        return user;
    }
}

Application应用

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);
    }

}

Repository Interface存储库接口

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> {}

Entity实体

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;
    }
}

Browser Page https://ibb.co/KDsqLn3浏览器页面https://ibb.co/KDsqLn3

With @ComponentScan(basePackages = "com.sample.user") you are overwriting the default behavior.使用@ComponentScan(basePackages = "com.sample.user")您正在覆盖默认行为。

So either remove this and put the packages below the package where you have @SpringBootApplication or add all packages to @ComponentScan .因此,要么删除它并将包放在 package 下你有@SpringBootApplication或将所有包添加到@ComponentScan

I recommend not to use the default Spring Boot behavior.我建议不要使用默认的 Spring 引导行为。

So remove ComponentScan an move RestServicesApplication to the com.sample package所以删除ComponentScan移动RestServicesApplicationcom.sample package

Use this it will resolve your Issue.使用它可以解决您的问题。

@ComponentScan(basePackages = "com.sample")

or use multiple package sacn somthing like或使用多个 package sacn 之类的

@ComponentScan({ "com.sample", "com.sample.user" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM