所以我最近开始学习 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
答案 0 :(得分:0)
我可以重现您的问题...(使用 Eclipse STS 4.9.0)。
当作为“Spring boot run configuration”(在eclipse中)运行时,上下文出现,但有一个不错的警告。我可以重现 404 错误页面(由于缺少 /error
映射!)。
我删除了(无用的)scanBasePackages
,发现上下文没有启动。 (具有所描述的问题)。
在 build.gradle
中,我添加了:
plugins {
...
id "com.diffplug.eclipse.apt" version "3.26.0" // Only for Eclipse
}
..如https://mapstruct.org/documentation/stable/reference/html/#_gradle
所述然后我(认识到,没有生成 mapperImpls)尝试通过 gradle :bootRun
运行并在您的映射器中发现以下生成/“编译”错误:
CategoryMapper
中,它必须是 "description"
not "descripcion"
。productos
中缺少 Categoria
的 Getter/Setter。categoria
中缺少 Producto
的 Getter/Setter。应用这些修复(和运行(至少最初/每次,当 mapper/dto 更改时)with gradle),应该*启动应用程序并授予对 loclahost:8080/platzi-market/api/products/all
的访问权限.
*:我在一个空的(不同的/h2/内存中)数据库上“测试”。 (这意味着,可能存在运行时错误。)
热烈欢迎spring!
查看您的存储库,我可以向您推荐: