简体   繁体   English

调用尝试浏览到休息控制器时的Spring Boot 404

[英]Spring Boot 404 when calling trying to browse to rest controller

I have seen many similar problems on here but none of them seem to be the solution for me. 我在这里看到过许多类似的问题,但似乎没有一个对我来说是解决方案。 My package structure seems to be correct and even if it wasn't, explicitly declaring the package or @RestController class in the application class still results in a 404. One thing I have noticed different about my problem than most others I have seen is that I am getting an HTML 404 response while most others I've seen are getting JSON 404 responses. 我的程序包结构似乎是正确的,即使不是正确的,在应用程序类中显式声明程序包或@RestController类仍会导致404。我注意到我的问题与大多数其他问题有所不同的一件事是我收到HTML 404响应,而我看到的大多数其他人都得到JSON 404响应。 I am not deploying this application, just running with mvn spring-boot:run . 我没有部署此应用程序,只是使用mvn spring-boot:run

Here is my code: 这是我的代码:

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

Adding the annotation @ComponentScan(@ComponentScan(basePackageClasses = UserController.class) still results in the 404 添加注释@ComponentScan(@ComponentScan(basePackageClasses = UserController.class)仍会导致404

Here is my rest controller class 这是我的休息控制器课

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

As you can see my Rest controller is in a nested class of the Spring application class. 如您所见,我的Rest控制器在Spring应用程序类的嵌套类中。 The folder structure is the same 文件夹结构相同

- src/main/java
  - com/billbuddy
    - BillbuddyApplication.java
    - controllers
        - UserController.java

Here is my pom.xml 这是我的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>

Here is what my output looks like when running the command (external IP of my CouchBase host [connection initialized but not yet used by my spring-boot application] has been censored and appears as XXX.XXX.XXX.XXX). 这是运行命令时我的输出的样子(我的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}]

I am testing the @RestController by browsing to http://localhost:8080/user/login in chrome and see the following screen: https://imgur.com/a/42U3eZR 我正在通过浏览到@RestControllerhttp:// localhost:8080 / user / login来测试@RestController ,并看到以下屏幕: https : //imgur.com/a/42U3eZR

Running 跑步

curl http://localhost:8080/user/login 

Returns 退货

<!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>

I'm sure it's going to be something stupid I'm doing, but I've tried dozens of other answers I've seen both on stack overflow and other websites to no avail. 我敢肯定,这将是我正在做的愚蠢的事情,但是我已经尝试了很多其他的答案,这些问题我在堆栈溢出和其他网站上都看不到。 Please help me. 请帮我。

Made a quick test with a project on my pc... you should have in your pom this dependency: 对我PC上的一个项目进行了快速测试...您应该在pom中具有此依赖关系:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

let me know if it works. 让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM