2

So I've started learning Spring recently by following a small course where we created a market api, when we started we created a simple hello world endpoint to test things out. Recently we just created an endpoint for accessing a list of products, but it seems all requests return a 404 error, since this error seems to be related with the controller I don't think posting all my code is needed.

This is my controller ProductController.java, I've only added the mapping for the first two methods (since I'm trying to fix this error still)


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);
    }
}

I also had to deal with a bean not being found which translated domain objects into dto's (and vise versa) by using MapStruct with the following error:

I made sure to annotate my interface with @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.

I managed to fix this with this (source from another student's comment)


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);
    }
}

But I'm not sure if this could be causing some interference with the controller class.

Are you accessing the correct endpoint?

This is my application.properties:

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

And this is the active dev profile (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

So the endpoint for accesing all product in my controller should be: localhost:8080/platzi-market/api/products/all which is returning a 404

I also checked if maybe I am using https so I made sure to use http:// in Postman, which also returned a 404

I double-checked the output in the terminal just to make sure the right port and context-path are being used:

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)

If you would like to check the rest of the code here's a link to the repo: https://github.com/Je12emy/spring-market-api, hope this makes since since I'm going crazy with error XD

Jeremy
  • 1,447
  • 20
  • 40
  • I'm not a Spring Boot user, but `scanBasePackages` likely wants a _package_ name not a _class_ name. Should it be `scanBasePackages = { "com.platzi.market.persistance.mapper", }` – Jim Garrison Feb 24 '21 at 00:19
  • Didn't notice, 1 less thing to worry about, thanks! – Jeremy Feb 24 '21 at 00:27

1 Answers1

0

I could reproduce your problem... (with Eclipse STS 4.9.0). When running as "Spring boot run configuration" (in eclipse) the context comes up, but with a decent warning. I can reproduce the 404 error pages (due to missing /error mapping!).

  1. I removed the (useless)scanBasePackages, and found the context not starting. (with the described problems).

  2. To build.gradle, I added:

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

    ..as described by https://mapstruct.org/documentation/stable/reference/html/#_gradle

  3. Then I (recognized, that no mapperImpls were generated,) tried to run via gradle :bootRun and found following generation/"compilation" errors in your mappers:

    1. In CategoryMapper it must be "description" not "descripcion".
    2. Missing Getter/Setter for productos in Categoria.
    3. Missing Getter/Setter for categoria in Producto.

Applying these fixes, (& running (at least initially/every time, when mapper/dto changes) with gradle), should* bring up the application and give access to loclahost:8080/platzi-market/api/products/all.

*: I "tested" on an empty (different/h2/in-memory) database. (It means, there can be runtime bugs.)


A warm welcome to a !

Reviewing your repo, I can recommend you:

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Hello, I just added those changes to the DTO classes and the mapper, but the problems still persists, how can I view the compilation errors from VSCode?. I'll try out Eclipse just to make sure VSCode is not causing any problems. (Hope it isn't since I really like it lol). Crazy how the error is not being caused by the controller, but the other layers instead I expected to fix this kind of errors when exceptions may be thrown. PS: Thanks for welcoming me into Spring, really like it over Laravel and dotNet so far! – Jeremy Feb 24 '21 at 03:33
  • Just tried running the application in Eclipse with the same result, could be a database connection error, I'll try out sqllite just to make sure – Jeremy Feb 24 '21 at 03:53
  • you can see the errors, when you start a gradle build (e.g. `./gradlew bootRun` from any IDE/terminal/shell): `" error: Unknown property "descriction" in result type Categoria. Did you mean "description"?"` – xerx593 Feb 24 '21 at 03:58
  • sql lite in spring boot is [possible](https://www.baeldung.com/spring-boot-sqlite) ... but not as "out of the box" as (e.g.) [h2](https://www.baeldung.com/spring-boot-h2-database), hsql, ... – xerx593 Feb 24 '21 at 03:58
  • mapstruct in VScode ... you'd have to wait for it: https://mapstruct.org/documentation/ide-support/ – xerx593 Feb 24 '21 at 03:59
  • Just tried `./gradlew bootRun` (I'll be using it more often to catch this kind of errors from now on, tnks), I' try out H2 since it may need less work to implement. – Jeremy Feb 24 '21 at 04:22