简体   繁体   中英

Spring Boot JPA Mysql AWS Not found on RequestMapping

I'm actually working with a Mysql database on AWS and Spring Boot with Maven + JPA. Important to mention that the app is connecting successfully to the database but when I'm calling the get 'Usuario':

http://localhost:8080/usuario

I receive:

{
    "timestamp": 1518993836210,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/usuario"
}

And it happens the same with all the other calls. Please help!! thanks!

application.app

spring.datasource.url=jdbc:mysql://..
spring.datasource.username=usr
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true

Usuario.java

package usuario;

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

@Entity
@Table(name="Usuario")
public class Usuario {

    @Id
    private String rut;
    private String nombre;

    public Usuario() {

    }
    public Usuario(String rut, String nombre) {
        super();
        this.rut=rut;
        this.nombre=nombre;
    }

    public String getRut() {
        return rut;
    }
    public void setRut(String rut) {
        this.rut = rut;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
}

UsuarioController.java

package usuario;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UsuarioController {

    @Autowired
    private UsuarioService usuarioService;

    @RequestMapping("/usuario")
    public List<Usuario> Usuario() {
        return usuarioService.getAllUsuarios();
    }

    @RequestMapping("/usuario/{rut}")
    public Usuario getUsuario(@PathVariable String rut) {
        return usuarioService.getUsuario(rut);
    }


    @RequestMapping(method=RequestMethod.POST, value="/usuario/update/{rut}" )
    public void addUsuario(@RequestBody Usuario usuario) {
        usuarioService.addUsuario(usuario);
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/usuario/borrar/{rut}" )
    public void deleteUsuario(@PathVariable String rut) {
        usuarioService.deleteUsuario(rut);  
    }


}

And UsuarioService.java

package usuario;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UsuarioService {

    @Autowired
    private UsuarioRepository usuarioRepository;

    public List<Usuario> getAllUsuarios(){
        List<Usuario> usuario = new ArrayList<>();
/*      usuarioRepository.findAll()
        .forEach(usuario::add);*/
        usuarioRepository.findAll().forEach(usuario::add);
        return usuario;
    }

    public Usuario getUsuario(String rut) { 
        return usuarioRepository.findByRut(rut);
    }

    public void addUsuario(Usuario usuario) {
         usuarioRepository.save(usuario);
    }

    public void updateUsuario(Usuario usuario) {
        usuarioRepository.save(usuario);
    }

    public void deleteUsuario(String id) {
        usuarioRepository.delete(id);

    }
}

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

    <groupId>com.springjpa</groupId>
    <artifactId>springJPA-postgreSQL</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringJPA-PostgreSQL</name>
    <description>Demo project for Spring Boot JPA - PostgreSQL</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.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>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

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

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


</project>

When I start the app, this is the console output:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.7.RELEASE)

feb 19, 2018 12:45:17 PM org.apache.catalina.core.StandardService startInternal
INFORMACIÓN: Starting service [Tomcat]
feb 19, 2018 12:45:17 PM org.apache.catalina.core.StandardEngine startInternal
INFORMACIÓN: Starting Servlet Engine: Apache Tomcat/8.5.15
feb 19, 2018 12:45:17 PM org.apache.catalina.core.ApplicationContext log
INFORMACIÓN: Initializing Spring embedded WebApplicationContext
feb 19, 2018 12:45:18 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
feb 19, 2018 12:45:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.12.Final}
feb 19, 2018 12:45:18 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
feb 19, 2018 12:45:19 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
feb 19, 2018 12:45:19 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
feb 19, 2018 12:45:34 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
feb 19, 2018 12:45:35 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
feb 19, 2018 12:45:35 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

This is my main class:

package com.springjpa;

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

@SpringBootApplication
public class SpringJpaMySqlApplication {

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

Your package name is very simple. You're getting 404 response code on trying to access the RequestMapping path. I believe you have servlet mapping issue. I see you didn't mention Starting class of an Application in question part. Hopefully It suppose to be in usuario package otherwise it is servlet mapping issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM