java - Spring Boot JPA Mysql AWS 在 RequestMapping 上找不到

标签 java mysql spring-boot request-mapping

我实际上正在使用 AWS 上的 Mysql 数据库以及带有 Maven + JPA 的 Spring Boot。 值得一提的是,该应用程序已成功连接到数据库,但是当我调用 get 'Usuario' 时:

http://localhost:8080/usuario

我收到:

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

所有其他调用都会发生同样的情况。请帮忙!!谢谢!

应用程序.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);  
    }


}

和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);

    }
}

这是我的 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>

当我启动应用程序时,这是控制台输出:

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

这是我的主课:

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);
    }
}

最佳答案

你的包名很简单。您在尝试访问 RequestMapping 路径时收到 404 响应代码。我相信您有 servlet 映射问题。我发现您在问题部分中没有提到应用程序的启动类。希望它位于 usuario 包中,否则就是 servlet 映射问题。

关于java - Spring Boot JPA Mysql AWS 在 RequestMapping 上找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48857445/

相关文章:

mysql - 对 3 个表进行 SQL 查询(计数)

mysql - Cygnus 不保留 MySql 数据库中的数据

java - 如何使用 JUnit 5 在 Spring Boot 中捕获 Hibernate ConstraintViolationException(或 Spring DataIntegrityViolationException?)

java - Embeddable 和 EmbeddedId 之间的 JPA 映射 @ManyToOne

java - 如何在 Android Studio 中更新 Cordova 版本

java - 输出 Java map 并在 HTML 中混合

php - 转换数组取字符串

java - 我的计时器有什么问题吗?

Java zip 字符编码

java - 使用 spring-boot gradle-plugin 构建的可执行 jar 缺少依赖项