带有springboot的404 Rest Api

时间:2018-12-05 16:17:03

标签: java rest api spring-boot microservices

我已经有几天了,尽管我知道他们可能会看到问题的标题重复,但事实是我已经尝试了几种对其他人有用的解决方案,但对我却没有。最可能的是,它真的很傻,我还没有看到它,但是我处于边缘。

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

}

答案 2 :(得分:0)

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

在映射路径时,您应该在

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

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

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