当前位置: 首页 > 知识库问答 >
问题:

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

锺离辰沛
2023-03-14

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

package com.ecommerce.microcommerce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.ecommerce.microcommerce.repository")
public class MicrocommerceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicrocommerceApplication.class, args);
    }
}

用户库:

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.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecommerce.micocommerce.repository.UserRepository;
import com.ecommerce.microcommerce.model.User;

@Service
@Transactional
public class UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    public void saveUser(User user) {
        userRepository.save(user);
    }

用户控制器:

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

我注意到,由于我添加了@ComponentScan注释,postman返回了404。但我必须使用这个,否则“自动连线”就不起作用了。谢谢你的帮助。

共有2个答案

齐才艺
2023-03-14

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

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

双弘益
2023-03-14

您的@ComponentScan没有扫描您的Controller也没有扫描您的Service。事实上,在使用@SpringBootApplication时根本不需要它。这应该扫描它是com.ecommerce.microcommerce的一部分的所有包。

可能由于包名上的类型,您的存储库未被扫描/自动连线micocommercevsmicrocommerce

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

@RestController
public class UserController {
    
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    // methods
}

 类似资料:
  • 我尝试使用QueryDSL学习Spring Data JPA教程 我的代码版本如下:spring-boot=1.3.8。release query-dsl=3.7.4 我的存储库如下 当我编译和构建代码时,下面的错误是show 我的错误与下面问题中给出的类似,但问题是它不是答案 https://stackoverflow.com/A/37800554/5192019

  • 我正在将Spring 4与Hibernate 4一起使用,并尝试配置多个数据源。当我使用新的第二个时,我总是收到错误。 异常org . spring framework . web . util . nestedservletexception:请求处理失败;嵌套异常为org . hibernate . hibernate exception:未找到当前线程的会话org . spring fram

  • 我正在使用ehcache和Spring。最近注意到以下错误。现在,XSD文件在http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd. 请告诉我是否有在本地加载XSD的解决方案。 原因:java。伊奥。FileNotFoundException:http://

  • 我在一天中做了一些API调用,但在使用Adobe Sign的新设置时遇到了问题。我可以得到请求很好,但得到HTTP404时,尝试任何张贴或放入他们的服务器。例如,到这里.... 当然,$HttpCode返回404,如上图所示。我为邮局做错什么了吗?

  • 我正在尝试学习Spring Security性,第一个代码示例是在运行URL“http://localhost:8080/spring-security-helloworld-xml/welcome”并使用jetty插件作为服务器时出现这样的错误。 错误:org.springframework.web.servlet.pageNotFound noHandlerFound警告:在名为“mvc-di

  • 从一个使用spring、JPA和JDBC连接到mysql数据库的程序开始,我试图将该应用程序配置为在嵌入式模式下使用H2数据库。 使用MYSQL一切正常,但使用H2则不然。 我无法让H2返回任何记录,尽管记录在那里,所以如果我通过JDBC进行相同的查询,如果我看到它们。 我的配置如下: 因此,如果我编写以下代码: 当我使用JDBC时,如果记录存在,我会在日历表中看到它,但当使用JPA时,它似乎没有