因此,我最近开始学习spring,学习了一个小课程,在这个课程中,我们创建了一个market api,开始时,我们创建了一个简单的helloworld端点来测试东西。最近我们刚刚创建了一个用于访问产品列表的端点,但似乎所有请求都返回404错误,因为这个错误似乎与控制器有关,我认为不需要发布所有代码。
这是我的控制器 ProductController.java
,我只为前两个方法添加了Map(因为我仍在尝试修复此错误)
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处理一个未找到的bean,该bean将域对象转换为dto(反之亦然),并出现以下错误:
我确保用 @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://in,这也返回了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)
如果您想检查代码的其余部分,这里有一个到回购的链接:https://github.com/je12emy/spring-market-api,希望这能让我发疯,因为我对错误xd
1条答案
按热度按时间9fkzdhlc1#
我可以重现你的问题(使用eclipsests4.9.0)。当作为“spring boot run configuration”(在eclipse中)运行时,会出现上下文,但会有一个适当的警告。我可以重现404错误页(由于缺少
/error
Map!)。我去掉了(无用的)
scanBasePackages
,发现上下文未启动(与所描述的问题)。至
build.gradle
,我补充道:…如所述https://mapstruct.org/documentation/stable/reference/html/#_gradle
然后我(意识到没有生成mapperimpls,)尝试通过
gradle :bootRun
并在Map程序中发现以下生成/编译错误:在
CategoryMapper
一定是的"description"
不"descripcion"
.缺少的getter/setter
productos
在Categoria
.缺少的getter/setter
categoria
在Producto
.使用gradle应用这些修复程序(至少在最初/每次mapper/dto更改时运行)(&running),应该*启动应用程序并授予对
loclahost:8080/platzi-market/api/products/all
.热烈欢迎来到Spring!
回顾您的回购协议,我可以向您推荐:
(下一步)创建一些测试(用于Map器、repos、控制器。。。代码覆盖率)
可能是 Spring 启动加载初始数据。
当然不要:在公开回购中公开(真实的)产品凭证
...