Home > Back-end >  An APRI REST http request goes in 404
An APRI REST http request goes in 404

Time:04-10

I am new with SpringBoot, and API REST, i used to develop in a Struts MVC SOAP project.

When i try to run an API REST on a browser or with curl it goes in 404.

here is my projec, the ide that I am using is IntelliJ IDEA 2021.3.3 (Community Edition):

info about the ide

IntelliJ IDEA 2021.3.3 (Community Edition)
Build #IC-213.7172.25, built on March 15, 2022
Runtime version: 11.0.14.1 1-b1751.46 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1776M
Cores: 8
Non-Bundled Plugins:
    org.jetbrains.kotlin (213-1.6.20-release-275-IJ6777.52)
    dev.flikas.idea.spring.boot.assistant.plugin (0.2.4)

Kotlin: 213-1.6.20-release-275-IJ6777.52

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.restweek</groupId>
    <artifactId>restweekend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>restweekend</name>
    <description>Demo project for Spring Boot Rest</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Controller

package com.restweek.restweekend.controller;

import com.restweek.restweekend.elementi.Product;
import com.restweek.restweekend.services.implementazioni.RichiestaImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/radice")
public class ControllerProva {

    @Autowired
    private RichiestaImpl richiesta;

    @GetMapping("/product")
    public List<Product> getProduct()
    {
        List<Product> products = richiesta.findAll();
        return products;
    }
}

The Object

package com.restweek.restweekend.elementi;

public class Product {
    private int id;
    private String pname;
    private String batchno;
    private double price;
    private int noofproduct;

    public Product() {

    }

    public Product(int id, String pname, String batchno, double price, int noofproduct) {
        super();
        this.id = id;
        this.pname = pname;
        this.batchno = batchno;
        this.price = price;
        this.noofproduct = noofproduct;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public String getBatchno() {
        return batchno;
    }

    public void setBatchno(String batchno) {
        this.batchno = batchno;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getNoofproduct() {
        return noofproduct;
    }

    public void setNoofproduct(int noofproduct) {
        this.noofproduct = noofproduct;
    }
}

Interface

package com.restweek.restweekend.services.interfaccie;

import com.restweek.restweekend.elementi.Product;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface Richiesta {

    List<Product> findAll();

}

Implementation

package com.restweek.restweekend.services.implementazioni;


import com.restweek.restweekend.elementi.Product;
import com.restweek.restweekend.services.interfaccie.Richiesta;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RichiestaImpl implements Richiesta {

    @Override
    public List<Product> findAll()
    {
        ArrayList<Product> products = new ArrayList<Product>();
        products.add(new Product(100, "Mobile", "CLK98123", 9000.00, 6));
        products.add(new Product(101, "Smart TV", "LGST09167", 60000.00, 3));
        products.add(new Product(102, "Washing Machine", "38753BK9", 9000.00, 7));
        products.add(new Product(103, "Laptop", "LHP29OCP", 24000.00, 1));
        products.add(new Product(104, "Air Conditioner", "ACLG66721", 30000.00, 5));
        products.add(new Product(105, "Refrigerator ", "12WP9087", 10000.00, 4));
        return products;
    }
}

Start Class

package com.restweek.restweekend.start;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestweekendApplication {

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

}

curl response in cmd

C:\Users\franc>curl http://localhost:8080/radice/product
{"timestamp":"2022-04-09T14:58:34.741 00:00","status":404,"error":"Not Found","path":"/radice/product"}
C:\Users\franc>

Did I put all that is required to understand the problem?

CodePudding user response:

The issue is that your beans are not even being picked up and created by the Spring Framework. Move RestweekendApplication to the package com.restweek.restweekend as follows:

package com.restweek.restweekend;

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

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

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.

  • Related