我在这里看到了许多类似的问题,但是似乎没有一个对我来说是解决方案。我的程序包结构似乎是正确的,即使不是正确的,在应用程序类中显式声明程序包或@RestController
类仍然会导致404。我注意到我的问题与大多数其他问题有所不同看到的是我收到HTML 404响应,而我看到的大多数其他人都得到JSON 404响应。我不会部署此应用程序,而只是运行mvn spring-boot:run
。
这是我的代码:
package com.billbuddy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BillbuddyApplication {
public static void main(String[] args) {
SpringApplication.run(BillbuddyApplication.class, args);
}
}
添加注释@ComponentScan(@ComponentScan(basePackageClasses = UserController.class)
仍会导致404
这是我的休息控制器课
package com.billbuddy.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login")
public String login() {
return "Hello, World";
}
}
您可以看到我的Rest控制器在Spring应用程序类的嵌套类中。文件夹结构相同
- src/main/java
- com/billbuddy
- BillbuddyApplication.java
- controllers
- UserController.java
这是我的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testprojects</groupId>
<artifactId>billbuddy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>billbuddy</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是运行命令时输出的内容(我的CouchBase主机的外部IP [已初始化连接但未由spring-boot应用程序使用的连接]已被检查,并显示为XXX.XXX.XXX.XXX)。
2018-12-22 06:11:42.603 INFO 56870 --- [ main] com.billbuddy.BillbuddyApplication : Starting BillbuddyApplication on charlies-mbp.lan with PID 56870 (/Users/user1/Projects/Github/angular-example/Server/target/classes started by user1 in /Users/user1/Projects/Github/angular-example/Server)
2018-12-22 06:11:42.608 INFO 56870 --- [ main] com.billbuddy.BillbuddyApplication : No active profile set, falling back to default profiles: default
2018-12-22 06:11:43.737 INFO 56870 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2018-12-22 06:11:43.921 INFO 56870 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 172ms. Found 1 repository interfaces.
2018-12-22 06:11:44.702 INFO 56870 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-22 06:11:44.736 INFO 56870 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-22 06:11:44.736 INFO 56870 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-22 06:11:44.749 INFO 56870 --- [ 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: [/Users/user1/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-12-22 06:11:44.840 INFO 56870 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-22 06:11:44.840 INFO 56870 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2130 ms
2018-12-22 06:11:45.345 INFO 56870 --- [ main] com.couchbase.client.core.CouchbaseCore : CouchbaseEnvironment: {sslEnabled=false, sslKeystoreFile='null', sslTruststoreFile='null', sslKeystorePassword=false, sslTruststorePassword=false, sslKeystore=null, sslTruststore=null, bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, bootstrapHttpDirectPort=8091, bootstrapHttpSslPort=18091, bootstrapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=4, computationPoolSize=4, responseBufferSize=16384, requestBufferSize=16384, kvServiceEndpoints=1, viewServiceEndpoints=12, queryServiceEndpoints=12, searchServiceEndpoints=12, configPollInterval=2500, configPollFloorInterval=50, networkResolution=NetworkResolution{name='auto'}, ioPool=NioEventLoopGroup, kvIoPool=null, viewIoPool=null, searchIoPool=null, queryIoPool=null, analyticsIoPool=null, coreScheduler=CoreScheduler, memcachedHashingStrategy=DefaultMemcachedHashingStrategy, eventBus=DefaultEventBus, packageNameAndVersion=couchbase-java-client/2.7.1 (git: 2.7.1, core: 1.7.1), retryStrategy=BestEffort, maxRequestLifetime=75000, retryDelay=ExponentialDelay{growBy 1.0 MICROSECONDS, powers of 2; lower=100, upper=100000}, reconnectDelay=ExponentialDelay{growBy 1.0 MILLISECONDS, powers of 2; lower=32, upper=4096}, observeIntervalDelay=ExponentialDelay{growBy 1.0 MICROSECONDS, powers of 2; lower=10, upper=100000}, keepAliveInterval=30000, continuousKeepAliveEnabled=true, keepAliveErrorThreshold=4, keepAliveTimeout=2500, autoreleaseAfter=2000, bufferPoolingEnabled=true, tcpNodelayEnabled=true, mutationTokensEnabled=false, socketConnectTimeout=1000, callbacksOnIoPool=false, disconnectTimeout=25000, requestBufferWaitStrategy=com.couchbase.client.core.env.DefaultCoreEnvironment$4@5d52e3ef, certAuthEnabled=false, coreSendHook=null, forceSaslPlain=false, compressionMinRatio=0.83, compressionMinSize=32, compressionEnabled=true, operationTracingEnabled=true, operationTracingServerDurationEnabled=true, tracer=ThresholdLogTracer, orphanResponseReportingEnabled=true, orphanResponseReporter=DefaultOrphanResponseReporter, keyValueServiceConfig=KeyValueServiceConfig{minEndpoints=1, maxEndpoints=1, pipelined=true, idleTime=0}, queryServiceConfig=QueryServiceConfig{minEndpoints=1, maxEndpoints=1, pipelined=false, idleTime=300}, searchServiceConfig=SearchServiceConfig{minEndpoints=0, maxEndpoints=12, pipelined=false, idleTime=300}, viewServiceConfig=ViewServiceConfig{minEndpoints=1, maxEndpoints=1, pipelined=false, idleTime=300}, analyticsServiceConfig=AnalyticsServiceConfig{minEndpoints=0, maxEndpoints=12, pipelined=false, idleTime=300}, queryTimeout=7500, viewTimeout=7500, searchTimeout=75000, analyticsTimeout=75000, kvTimeout=2500, connectTimeout=5000, dnsSrvEnabled=false, propagateParentSpan=true}
2018-12-22 06:11:46.672 INFO 56870 --- [ cb-io-1-1] com.couchbase.client.core.node.Node : Connected to Node XXX.XXX.XXX.XXX/XXX.XXX.XXX.XXX
2018-12-22 06:11:46.796 INFO 56870 --- [-computations-4] c.c.c.core.config.ConfigurationProvider : Selected network configuration: default
2018-12-22 06:11:46.810 INFO 56870 --- [-computations-4] c.c.c.core.config.ConfigurationProvider : Opened bucket BillBuddy
2018-12-22 06:11:47.394 INFO 56870 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-22 06:11:47.398 INFO 56870 --- [ main] com.billbuddy.BillbuddyApplication : Started BillbuddyApplication in 5.436 seconds (JVM running for 6.733)
2018-12-22 06:12:25.329 WARN 56870 --- [ cb-orphan-1] c.c.c.c.t.DefaultOrphanResponseReporter : Orphan responses observed: [{"top":[{"r":"XXX.XXX.XXX.XXX:8092","s":"view","c":"09F46A832158B8F9/0000000037386FDF","l":"192.168.86.142:62096"}],"service":"view","count":1}]
我正在通过浏览至chrome中的http://localhost:8080/user/login来测试@RestController
,并看到以下屏幕:
https://imgur.com/a/42U3eZR
运行
curl http://localhost:8080/user/login
返回
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1></body></html>
我确定这将是一件愚蠢的事情,但是我尝试了很多其他的答案,这些问题我在堆栈溢出和其他网站上都看不到。请帮助我。
答案 0 :(得分:2)
使用我的PC上的项目进行快速测试...您应该在pom中具有这种依赖性:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
让我知道它是否有效。