Error creating a Restful Web Services with Spring Boot - Whitelabel Error Page -

Asked

Viewed 2,178 times

1

I am trying to create a Restful Web Services with Spring Boot, however my page is showing the following error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue May 07 16:58:58 BRT 2019
There was an unexpected error (type=Not Found, status=404).
No message available

Follows my classes:

pom.xml:

<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.br.estudos.gastos</groupId>
<artifactId>gastos</artifactId>
<version>0.0.1-SNAPSHOT</version>

 <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.2.RELEASE</version> 
 </parent> 

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

model:

public class Gasto {

private String descricao;
private double valor;
private long codigoUsuario;

public Gasto(String descricao, double valor, long codigoUsuario) {
    this.descricao = descricao;
    this.valor = valor;
    this.codigoUsuario = codigoUsuario;
}

 //getters e setters

Resource:

@RestController
public class GastoResource {

private Map<Integer, Gasto> gastos;

public GastoResource() {
    gastos = new HashMap<Integer, Gasto>();
    Gasto g1 = new Gasto("Saraiva", 23.5, 1l);
    Gasto g2 = new Gasto("Starbucks", 27, 2l);
    Gasto g3 = new Gasto("Levis", 245.5, 3l);

    gastos.put(1, g1);
    gastos.put(2, g2);
    gastos.put(3, g3);
}

@RequestMapping(value = "/gastos", method = RequestMethod.GET)
public ResponseEntity<List<Gasto>> listar() {
    return new ResponseEntity<List<Gasto>>(new ArrayList<Gasto>(gastos.values()), HttpStatus.OK);
}

}

spring boot Configuration:

@SpringBootApplication
public class SpringBootConfiguration {

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

}

I run the project by the Springbootconfiguration class and apparently everything right, no error appears and says that Tomcat climbed on port 8080, but when access localhost:8080/expense, says that it did not find the address. Can someone help me? Thank you

  • 1

    Oops, post your Mports too.. as well as the Packages.. :)

  • 1

    Hello, you can post the hierarchy of your packages and classes?

  • I copied exactly how you posted and here the url localhost:8080/spend worked. How’s your class hierarchy ??

  • Post the package of each class as well. There is a concept called package scan which is influenced by the packages and which must be the cause of your problem. Basically, your class annotated with @SpringBootApplication should be at the same level or levels above your Controller class (there are ways to work with other package structures, but at the moment, I think it will be more disturbing than helping)

1 answer

1

Browser other questions tagged

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