【问题标题】:404 Rest Api with springboot404 Rest Api 带弹簧靴
【发布时间】:2019-05-07 06:26:30
【问题描述】:

我已经有几天的时间了,虽然我知道他们可能会看到问题的标题重复,但事实是我已经尝试了几种对其他人有效但对我无效的解决方案。最有可能的是,它真的很傻,我还没有看到它,但我在边缘。

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

<groupId>com.paises</groupId>
<artifactId>paises</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>paises</name>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath />
</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-jersey</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.44</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>23.2-jre</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.0.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

这是我的班级申请

package ar.com.paises;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class PaisesApplication  {

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

@RequestMapping("/")
String home() {
    return "Hello World!";
}
}

这是我的 .properties

spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://localhost:3306/test_avantrip?verifyServerCertificate=false&useSSL=false&requireSSL=false
server.port=8083
spring.profiles.active=@spring.profiles.active@

我收到的唯一消息如下

404 not found api

This is the guide that I was following 有什么建议吗?

更新

@RequestMapping(value="/hi", produces= {MediaType.TEXT_PLAIN_VALUE })
public String home() {
    return "Hello World!";
}

还没有

更新 2

我更改了端口,将其从 .properties 中删除并保留在 8080 中,我没有看到任何更改。此外,这里是我启动 Spring Boot 控制台时的消息

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

2018-12-05 14:07:29.519  INFO 1844 --- [           main] ar.com.paises.PaisesApplication          : Starting PaisesApplication on MIETURAW10 with PID 1844 (D:\Proyectos\avantrip\paises\target\classes started by mietura in D:\Proyectos\avantrip\paises)
2018-12-05 14:07:29.525  INFO 1844 --- [           main] ar.com.paises.PaisesApplication          : No active profile set, falling back to default profiles: default
2018-12-05 14:07:30.161  INFO 1844 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2018-12-05 14:07:30.182  INFO 1844 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 repository interfaces.
2018-12-05 14:07:30.734  INFO 1844 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-12-05 14:07:30.764  INFO 1844 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-05 14:07:30.764  INFO 1844 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-05 14:07:30.772  INFO 1844 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_161\bin;C:\apache-maven-3.5.3\\bin;C:\apache-maven-3.5.3\\bin;C:\gradle-4.0.1\bin;C:\Users\Etura Maria Ines\AppData\Local\Android\android-sdk\platform-tools;C:\Users\Etura Maria Ines\AppData\Local\Android\android-sdk\tools;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\Users\Etura Maria Ines\AppData\Local\Android\android-sdk\;8080;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\apache-ant-1.6.2\bin;C:\Program Files\PowerShell\6.0.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\oraclexe\app\oracle\product\11.2.0\server\bin;C:\Users\mietura\AppData\Local\Microsoft\WindowsApps;;.]
2018-12-05 14:07:31.138  INFO 1844 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-12-05 14:07:31.138  INFO 1844 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1549 ms
2018-12-05 14:07:31.310  INFO 1844 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-12-05 14:07:31.502  INFO 1844 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-12-05 14:07:31.546  INFO 1844 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2018-12-05 14:07:31.609  INFO 1844 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.7.Final}
2018-12-05 14:07:31.613  INFO 1844 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-12-05 14:07:31.875  INFO 1844 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2018-12-05 14:07:31.986  INFO 1844 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-12-05 14:07:32.185  INFO 1844 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-12-05 14:07:32.440  INFO 1844 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-05 14:07:32.444  INFO 1844 --- [           main] ar.com.paises.PaisesApplication 

【问题讨论】:

  • 检查你的端口,并添加 text/plain header 作为响应类型
  • 收到请求时 Spring 是否记录任何内容?
  • @mumpitz 不,这不是
  • @AlmasAbdrazak 端口是正确的,我通过控制台验证了它,我更新了我的问题,因为我将响应类型添加到 TEXT_PLAIN_VALUE 并且它仍然无法识别它
  • 可能还有其他可能的原因。你检查spring boot context加载成功并启动tomcat服务器了吗?与数据源相关的配置是否有效?

标签: java rest api spring-boot microservices


【解决方案1】:

尝试删除属性 spring.profiles.active=@spring.profiles.active@

【讨论】:

    【解决方案2】:

    @RequestMapping("/") 移动到控制器类,例如:

    @RestController
    public class Controls {
    
        @RequestMapping("/")
        public String home() {
           return "Hello World!";
        }
    
    }
    

    所以你的主要方法看起来像:

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

    【讨论】:

      【解决方案3】:

      如果您查看控制台日志,您将不会看到路径“/hi”没有被映射,这意味着 spring 无法找到您的控制器 bean。

      当路径被映射时,您应该会在

      中看到类似下面的内容
      RequestMappingHandlerMapping : Mapped "{[/hi],methods=[GET]}".
      

      请将剩下的 Controller 移到它自己的类中,它可以在同一个文件中,但将它移到一个新的不同的类中,如下所示:

      package ar.com.paises;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      
      @SpringBootApplication
      public class PaisesApplication  {
      
        public static void main(String[] args) {
          SpringApplication.run(PaisesApplication.class, args);
        }
      }
      
      @RestController
      class HiController {
      
        @RequestMapping("/hi")
        String home() {
          return "Hello World!";
        }
      }
      

      另外,请确保在您部署应用程序时,会显示之前的日志。

      根据您的端口调用端点的方式应该是:

      http://localhost:8083/hi

      【讨论】:

        猜你喜欢
        • 2019-05-03
        • 2015-07-04
        • 2018-02-14
        • 1970-01-01
        • 2016-02-16
        • 2014-06-27
        • 2021-07-15
        • 2021-02-16
        • 2020-05-18
        相关资源
        最近更新 更多