Spring Boot Project with 404 error

Asked

Viewed 550 times

3

This is my controller

package com.iape.cobranca.resource;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iape.cobranca.model.Conta;
import com.iape.cobranca.repository.ContaRepository;

@RestController
@RequestMapping("/contas")
public class ContaResource {

    @Autowired
    private ContaRepository contaRepository;

    @GetMapping
    public List<Conta> lista(){
        return contaRepository.findAll();
    }


}

This is my replacement;

package com.iape.cobranca.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.iape.cobranca.model.Conta;

public interface ContaRepository extends JpaRepository<Conta, Long> {

}

this is my entity;

package com.iape.cobranca.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "conta")
public class Conta {

    private Long codigo;
    private String nome;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getCodigo() {
        return codigo;
    }

    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Conta other = (Conta) obj;
        if (codigo == null) {
            if (other.codigo != null)
                return false;
        } else if (!codigo.equals(other.codigo))
            return false;
        return true;
    }



}

This is my pom.xml file

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.iape.cobranca.api</groupId>
    <artifactId>cobranca</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cobranca</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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


        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Migracoes - Flyway -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
    </dependencies>

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



</project>

That’s how I’m connecting to the bank;

spring.jpa.database=POSTGRESQL
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false


  spring.jpa.hibernate.ddl-auto=create

 spring.datasource.url=jdbc:postgresql://localhost:5432/boleto?createDatabaseIfNotExist=true&useSSL=false
 spring.datasource.username=postgres
 spring.datasource.password=postgres





   #server.port=8181

spring.jpa.show-sql=true


spring.jackson.deserialization.fail-on-unknown-properties=true

spring.jackson.date-format=yyyy-MM-dd

Why am I getting the 404 error?

  • Error 404 occurs when calling GET from /contas?

  • that’s right, exactly

  • @Dherik you could download my application and test on your computer?

  • Where the request is coming from, from the same server ?

  • How are you doing this request? Are you doing something like http://127.0.0.1:8080/contas?

  • You can put the class where the main of your project?

  • https://github.com/wladyband/cobranca/blob/master/cobranca/src/main/java/com/iape/cobranca/resource/ContaResource.java

Show 2 more comments

1 answer

1


I guess the Spring Boot scan isn’t seeing your endpoint.

For this, the main class needs to be at the root of the project, package package com.iape.cobranca. Put the class CobrancaApplication in this package and no longer in the package package com.iape.cobranca.api.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.