【问题标题】:Can't access API endpoint无法访问 API 端点
【发布时间】:2021-05-26 06:43:07
【问题描述】:

所以我最近开始学习 Spring,学习了一个创建市场 api 的小课程,当我们开始时,我们创建了一个简单的 hello world 端点来测试。最近我们刚刚创建了一个用于访问产品列表的端点,但似乎所有请求都返回 404 错误,因为这个错误似乎与控制器有关,我认为不需要发布我的所有代码。

这是我的控制器ProductController.java,我只添加了前两种方法的映射(因为我仍在尝试修复此错误)


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

@RestController
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/all")
    public List<Product> getAll() {
        return productService.getAll();
    }

    @GetMapping("/{productId}")
    public Optional<Product> getProduct(@PathVariable("productId") int productId) {
        return productService.getProduct(productId);
    }

    public Optional<List<Product>> getByCategory(int categoryId) {
        return productService.getByCategory(categoryId);
    }

    public Product save(Product product) {
        return productService.save(product);
    }

    public Boolean delete(int productId) {
        return productService.delete(productId);
    }
}

我还必须通过使用 MapStruct 来处理找不到将域对象转换为 dto(反之亦然)的 bean,但出现以下错误:

我确保用@Mapper(componentModel="spring")注释我的界面

***************************
APPLICATION FAILED TO START
***************************

Description:

Field mapper in com.platzi.market.persistance.ProductoRepository required a bean of type 'com.platzi.market.persistance.mapper.ProductMapper' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.platzi.market.persistance.mapper.ProductMapper' in your configuration.

我设法用这个解决了这个问题(来自另一个学生的评论)


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = { "com.platzi.market.persistance.mapper.ProductMapper", })
public class PlatziMarketApplication {
    public static void main(String[] args) {
        SpringApplication.run(PlatziMarketApplication.class, args);
    }
}

但我不确定这可能是否会对控制​​器类造成一些干扰。

您是否访问了正确的端点?

这是我的application.properties

spring.profiles.active=dev
server.servlet.context-path=/platzi-market/api

这是活动的dev 个人资料 (application-dev.properties)

server.port=8080

#  Database, values are altered
spring.datasource.url=jdbc:mysql://localhost:3306/platzi-market
spring.datasource.username=foo
spring.datasource.password=bar

所以在我的控制器中访问所有产品的端点应该是:localhost:8080/platzi-market/api/products/all,它返回一个 404

我还检查了我是否使用了 https,所以我确保在 Postman 中使用 http://,它也返回了 404

我仔细检查了终端中的输出,以确保使用了正确的端口和上下文路径:

2021-02-23 17:20:07.583  INFO 51334 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/platzi-market/api'
2021-02-23 17:20:07.594  INFO 51334 --- [           main] c.platzi.market.PlatziMarketApplication  : Started PlatziMarketApplication in 3.881 seconds (JVM running for 4.296)

如果您想查看其余代码,请点击此处的 repo 链接:https://github.com/Je12emy/spring-market-api,希望这是因为我因错误而发疯了 XD

【问题讨论】:

  • 我不是 Spring Boot 用户,但 scanBasePackages 可能想要一个 package 名称而不是 class 名称。应该是scanBasePackages = { "com.platzi.market.persistance.mapper", }
  • 没注意到,少了 1 件需要担心的事情,谢谢!

标签: java spring spring-boot rest


【解决方案1】:

我可以重现您的问题...(使用 Eclipse STS 4.9.0)。 当作为“Spring Boot 运行配置”(在 Eclipse 中)运行时,会出现上下文,但会发出一个不错的警告。我可以重现 404 错误页面(由于缺少 /error 映射!)。

  1. 我删除了(无用的)scanBasePackages,发现上下文没有启动。 (有描述的问题)。

  2. build.gradle,我补充说:

     plugins {
       ...
       id "com.diffplug.eclipse.apt" version "3.26.0" // Only for Eclipse
     }
    

    ..如https://mapstruct.org/documentation/stable/reference/html/#_gradle所述

  3. 然后我(认识到没有生成任何 mapperImpls)尝试通过 gradle :bootRun 运行并在您的映射器中发现以下生成/“编译”错误

    1. CategoryMapper 中必须是"description" 不是 "descripcion"
    2. Categoria 中缺少productos 的Getter/Setter。
    3. Producto 中缺少 categoria 的 Getter/Setter。

应用这些修复(& 运行(至少最初/每次,当 mapper/dto 更改时)使用 gradle),应该* 启动应用程序并授予对 loclahost:8080/platzi-market/api/products/all 的访问权限。

*:我在一个空的(不同的/h2/in-memory)数据库上“测试”。 (这意味着,可能存在运行时错误。)


热烈欢迎

查看你的回购,我可以推荐你:

【讨论】:

  • 您好,我刚刚将这些更改添加到 DTO 类和映射器中,但问题仍然存在,如何从 VSCode 中查看编译错误?我将尝试 Eclipse 以确保 VSCode 不会导致任何问题。 (希望不是因为我真的很喜欢它哈哈)。疯狂的错误不是由控制器引起的,而是其他层我希望在可能引发异常时修复这种错误。 PS:感谢你欢迎我进入 Spring,到目前为止真的很喜欢 Laravel 和 dotNet!
  • 刚刚尝试在 Eclipse 中运行应用程序,结果相同,可能是数据库连接错误,我将尝试使用 sqllite 以确保
  • 您可以在启动 gradle 构建时看到错误(例如,来自任何 IDE/终端/shell 的 ./gradlew bootRun):" error: Unknown property "descriction" in result type Categoria. Did you mean "description"?"
  • spring boot 中的 sql lite 是 possible ...但不像(例如)h2、hsql、...那样“开箱即用”
  • VScode 中的mapstruct ...你必须等待它:mapstruct.org/documentation/ide-support
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多