Can’t access API endpoint

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



Read more here: https://stackoverflow.com/questions/66342545/cant-access-api-endpoint

Content Attribution

This content was originally published by Jeremy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: