Home > database >  whitelabel error page 404 spring boot microservices Rest Controller
whitelabel error page 404 spring boot microservices Rest Controller

Time:11-08

I am new to Spring Boot and Microservices. I have created RestController and tried to check using RequestMapping test but it give us error as 404, whitelabel error page.

Below is pom.xml :

<modelVersion>4.0.0</modelVersion>
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.6</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demoweb</groupId>
<artifactId>CitizenDetailService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CitizenDetailService</name>
<description>Microservices Project for CitizenServiceDetailService</description>
<properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>2020.0.4</spring-cloud.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-web</artifactId>
  </dependency>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
<dependencyManagement>
   <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

</project> 

Below is the code for the controller

package com.exmaple.microservices.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/citizen")
public class CitizenController {
    @RequestMapping("/test")
    public String test()
    {
        return "Hello";
    }       
}

Below is Spring boot main class

package com.example.microservices.CitizenDetailsService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CitizenDetailServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(CitizenDetailServiceApplication.class, args);
    }    
}

Server Start up logs are mentioned below :

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.5.6)[0;39m

[2m2021-11-07 21:51:08.753[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m Starting CitizenDetailServiceApplication using Java 16.0.1 on DESKTOP-9G5CUP8 with PID 27720 (D:\Spring\CitizenDetailService\target\classes started by 91909 in D:\Spring\CitizenDetailService)
[2m2021-11-07 21:51:08.755[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-11-07 21:51:09.257[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2021-11-07 21:51:09.266[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.
[2m2021-11-07 21:51:09.451[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=c60b4e5b-8c67-3295-9bcc-48b6ccda1d6a
[2m2021-11-07 21:51:09.821[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8085 (http)
[2m2021-11-07 21:51:09.831[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-11-07 21:51:09.831[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.54]
[2m2021-11-07 21:51:09.968[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-11-07 21:51:09.968[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1184 ms
[2m2021-11-07 21:51:10.121[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2021-11-07 21:51:10.170[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 5.4.32.Final
[2m2021-11-07 21:51:10.327[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.hibernate.annotations.common.Version  [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
[2m2021-11-07 21:51:10.414[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2021-11-07 21:51:10.753[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2021-11-07 21:51:10.775[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
[2m2021-11-07 21:51:11.079[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[2m2021-11-07 21:51:11.090[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2021-11-07 21:51:11.128[0;39m [33m WARN[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[2m2021-11-07 21:51:11.619[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mDiscoveryClientOptionalArgsConfiguration[0;39m [2m:[0;39m Eureka HTTP Client uses RestTemplate.
[2m2021-11-07 21:51:11.802[0;39m [33m WARN[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36miguration$LoadBalancerCaffeineWarnLogger[0;39m [2m:[0;39m Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
[2m2021-11-07 21:51:11.879[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.n.eureka.InstanceInfoFactory      [0;39m [2m:[0;39m Setting initial instance status as: STARTING
[2m2021-11-07 21:51:11.941[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Initializing Eureka in region us-east-1
[2m2021-11-07 21:51:11.946[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.n.d.s.r.aws.ConfigClusterResolver     [0;39m [2m:[0;39m Resolving eureka endpoints via configuration
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Disable delta property : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Single vip registry refresh property : null
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Force full registry fetch : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application is null : false
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Registered Applications size is zero : true
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application version is -1: true
[2m2021-11-07 21:51:11.980[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Getting all instance registry info from the eureka server
[2m2021-11-07 21:51:12.272[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m The response status is 200
[2m2021-11-07 21:51:12.279[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Starting heartbeat executor: renew interval is: 30
[2m2021-11-07 21:51:12.287[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.n.discovery.InstanceInfoReplicator    [0;39m [2m:[0;39m InstanceInfoReplicator onDemand update allowed rate per min is 4
[2m2021-11-07 21:51:12.303[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Discovery Client initialized at timestamp 1636302072301 with initial instances count: 0
[2m2021-11-07 21:51:12.305[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.n.e.s.EurekaServiceRegistry       [0;39m [2m:[0;39m Registering application UNKNOWN with eureka with status UP
[2m2021-11-07 21:51:12.306[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Saw local status change event StatusChangeEvent [timestamp=1636302072306, current=UP, previous=STARTING]
[2m2021-11-07 21:51:12.308[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nfoReplicator-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_UNKNOWN/DESKTOP-9G5CUP8:8085: registering service...
[2m2021-11-07 21:51:12.330[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8085 (http) with context path ''
[2m2021-11-07 21:51:12.330[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36m.s.c.n.e.s.EurekaAutoServiceRegistration[0;39m [2m:[0;39m Updating port to 8085
[2m2021-11-07 21:51:12.346[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nfoReplicator-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m DiscoveryClient_UNKNOWN/DESKTOP-9G5CUP8:8085 - registration status: 204
[2m2021-11-07 21:51:12.388[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.e.m.C.CitizenDetailServiceApplication [0;39m [2m:[0;39m Started CitizenDetailServiceApplication in 4.053 seconds (JVM running for 4.652)
[2m2021-11-07 21:51:42.285[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Disable delta property : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Single vip registry refresh property : null
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Force full registry fetch : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application is null : false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Registered Applications size is zero : true
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application version is -1: false
[2m2021-11-07 21:51:42.286[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Getting all instance registry info from the eureka server
[2m2021-11-07 21:51:42.295[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[freshExecutor-0][0;39m [36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m The response status is 200
[2m2021-11-07 21:51:57.793[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2021-11-07 21:51:57.793[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2021-11-07 21:51:57.796[0;39m [32m INFO[0;39m [35m27720[0;39m [2m---[0;39m [2m[nio-8085-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 3 ms

Any help will be appreciated. Thanx in advance

CodePudding user response:

If you are getting a 404 error page, it means that SpringBoot does not have a request mapping from this URI path to a specific controller method.

This can happen for 2 main reasons ...

  1. You are using the wrong request URI
  2. You did not create a controller bean, which is mapped to this URI

Given that I see the code for the controller bean, this means its a wrong URI.

If you did not change any of the configurations in application.properties the path you should be using to get the data should be http://localhost:8080/citizen/test. Try it out with this one ... if it does not work ... please add the output, which SpringBoot prints when starting the app to the original question. Also add a comment to this answer, so I get notified and can respond.

CodePudding user response:

The issue is that your Controller is not being scanned and thus no Spring-managed Bean is created. Move your main CitizenDetailServiceApplication class to the package com.example.microservices, which should be your parent package for any other sub-packages you create.

package com.example.microservices;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CitizenDetailServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(CitizenDetailServiceApplication.class, args);
    }    
}

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes. The default value for @ComponentScan means that all the sub-packages on the package the @ComponentScan is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.

  • Related