【发布时间】: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