rasilvap
rasilvap

Reputation: 2159

Spring Boot Controllers not working 404 error

I'm developing a Spring boot application and my endpoints are not working at all. I'm getting the next issue:

{
    "timestamp": "2020-06-21T21:12:39.716+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/prduct/jjjk"
}

This is the structure of my project:

enter image description here

As you can see the controller is in the same path a subpackage of the Application class, but it is not scanned.

package com.mercadolibre.mutants.controllers;

import com.mercadolibre.mutants.entities.Product;
import com.mercadolibre.mutants.repositories.ProductRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

  @Autowired
  ProductRepository productRepository;

  @GetMapping("/prduct/{id}")
  public ResponseEntity<Product> findById(@PathVariable("id") String id) {
    Optional<Product> p = productRepository.findById(id);
    return ResponseEntity.ok(p.orElse(null));
  }

  @PostMapping(value = "/products", consumes = "application/json")
  public ResponseEntity<Product> save(@RequestBody Product p) {
    Product result = productRepository.save(p);
    return ResponseEntity.ok(result);
  }
}

This is my Application class:

package com.mercadolibre.mutants;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.mercadolibre.mutants")
public class MutantsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MutantsApplication.class, args);
    }

}

I'm not able to identify the issue or what I'm missing. Any ideas?

Upvotes: 1

Views: 433

Answers (0)

Related Questions