cjwillis48
cjwillis48

Reputation: 615

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. I am not deploying this application, just running with 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

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. The folder structure is the same

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

Here is my 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).

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://i.sstatic.net/bIwMM.jpg

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.

Upvotes: 0

Views: 349

Answers (1)

Lorelorelore
Lorelorelore

Reputation: 3393

Made a quick test with a project on my pc... you should have in your pom this dependency:

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

let me know if it works.

Upvotes: 2

Related Questions