[英]404 Rest Api with springboot
I already have a couple of days in this and although I know they may see the title of the question repeated, the truth is that I have already tried several solutions that have worked for others but not me.我已经有几天了,虽然我知道他们可能会看到重复的问题标题,但事实是我已经尝试了几种对其他人有效但对我无效的解决方案。 The most likely thing is that it's really very silly and I have not seen it yet but I'm on the edge.
最有可能的事情是它真的很愚蠢,我还没有看到它,但我处于边缘。
This is my pom.xml这是我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
4.0.0 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>
This is my class Application这是我的班级申请
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!";
}
}
And this is my .properties这是我的 .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@
The only message I receive is the following我收到的唯一消息如下
This is the guide that I was following Any suggestions?这是我遵循的指南 有什么建议吗?
Update
更新
@RequestMapping(value="/hi", produces= {MediaType.TEXT_PLAIN_VALUE })
public String home() {
return "Hello World!";
}
nothing yet还没有
Update 2
更新 2
I changed the port, removing it from the .properties and leaving it in 8080 I do not see any change.我更改了端口,将其从 .properties 中删除并将其保留在 8080 中,我看不到任何更改。 Additionally here are the messages of the spring boot console when I start it
另外这里是我启动时 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
尝试删除属性 spring.profiles.active=@spring.profiles.active@
Move the @RequestMapping("/")
to a controller class, something like :将
@RequestMapping("/")
到控制器类,例如:
@RestController
public class Controls {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
}
So your main method would look like :所以你的主要方法看起来像:
@SpringBootApplication
public class PaisesApplication {
public static void main(String[] args) {
SpringApplication.run(PaisesApplication.class, args);
}
}
If you take a look at the console logs you won't see that the path "/hi" is not being mapped, which means spring is not able to find your controller bean.如果您查看控制台日志,您将看不到路径“/hi”没有被映射,这意味着 spring 无法找到您的控制器 bean。
when a path is mapped you should see something like the following in当路径被映射时,你应该看到如下内容
RequestMappingHandlerMapping : Mapped "{[/hi],methods=[GET]}".
Please move the rest Controller to its own class, it can be in the same File but move it to a new different class something like the following:请将其余控制器移动到它自己的类,它可以在同一个文件中,但将它移动到一个新的不同类,如下所示:
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!";
}
}
Also, make sure that when you're deploying your application a log as the previous one appear.此外,请确保在部署应用程序时出现上一个日志。
The way you should call your endpoint according to your port should be:根据端口调用端点的方式应该是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.