Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在带有spring的postman中未找到Ressource错误_Java_Spring - Fatal编程技术网

Java 在带有spring的postman中未找到Ressource错误

Java 在带有spring的postman中未找到Ressource错误,java,spring,Java,Spring,我是个初学者,正在测试springboot,创建一个简单的api。 当通过邮递员测试我的post请求时,它给了我一个404错误。 用户存储库: package com.ecommerce.micocommerce.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.ecommerce.microcommerce.model.User; public interface U

我是个初学者,正在测试springboot,创建一个简单的api。 当通过邮递员测试我的post请求时,它给了我一个404错误。

用户存储库:

package com.ecommerce.micocommerce.repository;

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

import com.ecommerce.microcommerce.model.User;

public interface UserRepository extends JpaRepository<User, Long> {
    
}
用户控制器:

package com.ecommerce.microcommerce.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ecommerce.microcommerce.model.User;
import com.ecommerce.microcommerce.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;

    @PostMapping("/Users")
    public void addUser(@RequestBody User user) {
        System.out.println(user);
        userService.saveUser(user);
    }
}
application.properties:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8889/microcommerce?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root


#Hibernate
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
我注意到自从我添加了@ComponentScan注释后,postman给我返回了404。 但是我必须使用这个,否则@Autowired就不起作用了。
谢谢您的帮助。

您的
@ComponentScan
没有扫描您的
控制器
也没有扫描您的
服务
。 事实上,在使用
@SpringBootApplication
时根本不需要它。这应该扫描它所属的所有包,在您的情况下,
com.ecommerce.microcommerce

可能您的
存储库
没有被扫描/自动连线,因为您的包名上有一个类型?
micocommerce
vs
microcommerce


提示:使用构造函数注入而不是自动连接

@RestController
公共类用户控制器{
专用最终用户服务用户服务;
公共用户控制器(用户服务用户服务){
this.userService=userService;
}
//方法
}

编辑@ComponentScan(“com.ecommerce.microcommerce.repository”)@ComponentScan(“com.ecommerce.microcommerce”),以便它扫描基本包中的控制器、服务和存储库。请检查Springboot如何使用@Springboot应用程序注释(因为它已经启用了@ComponentScan功能):

请检查您为UserRepository类提供的包名(micocommerce)

package com.ecommerce.microcommerce.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ecommerce.microcommerce.model.User;
import com.ecommerce.microcommerce.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;

    @PostMapping("/Users")
    public void addUser(@RequestBody User user) {
        System.out.println(user);
        userService.saveUser(user);
    }
}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8889/microcommerce?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root


#Hibernate
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect