Spring Boot: Application is displaying 404 error - java

I am trying to run Spring Boot REST API application, but I am getting a 404 error.
package com.spring.boot.entry;
#SpringBootApplication
#ComponentScan(basePackageClasses = HelloController.class)
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
package com.spring.boot.entry.hello;
#RestController
public class HelloController {
#RequestMapping("/hello")
public String sayHello() {
return "Hi First Spring boot application ";
}
}
pom.xml
<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>
<groupId>io.spring.boot.quickstart</groupId>
<artifactId>course-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
I also tried #ComponentScan(basePackageClasses = HelloController.class),
but no luck. Could you please help to resolve this issue?
If I hit http://localhost:8080/hello url then I am getting 404.

I executed your code on my system and unless you've messed up with the directory structure it is working as expected. Cross check that once.
CourseApiApp.java
HelloController.java
Output

Related

Sping RestController not responding to request

I am just trying to implement a sample rest controller on spring boot for learning. When i am sending the request through web browser i am getting a white label error.
My applications context path is set to /learn. Below are my source files, project structure and pom.xml.
TestController.java
package com.practice.learn.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/learn")
public class TestController {
TestController(){
System.out.println("TestController initialized");
}
#GetMapping("/test")
public String testMethod() {
System.out.println("Inside test method");
return "This is sample text";
}
}
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 https://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.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
LearnApplication.java
package com.practice.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
application.properties
server.port=8081
server.servlet.context-path=/learn
since you already set the servlet context path as '/learn' in your configuration file, you do not need to add '/learn' again in the RequestMapping in your controller. just remove it and let us know.

Getting 403 when one spring boot api is called from a java client although security dependencies are not added

I have created a simple spring boot application, with a get api which return "Hello world". The api works fine when called from postman or browser. However when I am trying to consume the api via JAX-RX(Jercy or RestEasy) or spring boots Rest Template in a different application, it gives 403 Forbidden (Authentication required) error.
Note: - My spring boot application have dependency only on spring-boot-starter-web and no other security dependency is added.
Following things I have tried but did not work
a. Adding dependency spring-boot-starter-security
b. Disabling csrf and cors.
c. authorizeRequests/authorizeHttpRequests anyRequests PermitAll.
Spring Boot Application
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 https://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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringServerApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringServerApp</name>
<description>A simple spring rest server</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
Controller class
package com.example.SpringServerApp.controller;
import org.springframework.web.bind.annotation.*;
#RestController
public class MainController {
#GetMapping("/helloGet")
String getHello() {
return "Hello World";
}
}
Client Code in a different application to call the api
package org.example.restClient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class JercyClient {
private static final String GET_HELLO = "http://localhost:8080/helloGet";
private static final String SAMPLE_API = "https://catfact.ninja/fact";
static WebTarget webTarget;
static {
Client client = ClientBuilder.newClient();
webTarget = client.target(GET_HELLO);
}
public static String getHello() {
Response response = webTarget.request().get();
return response.readEntity(String.class);
}
}

Not able to access rest API in Spring Boot application

I have created a Spring Boot application with pom.xml as below
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tcs</groupId>
<artifactId>hotel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Hotel project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
And my Application class is in com.demo., and my controller is in the com.demo.controller package. Given below my controller class.
package com.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HotelController {
#RequestMapping("/test")
public String getString() {
return "Helloworld";
}
}
After running the Spring Boot application I have tried to access my rest API at localhost:8080/test, but the API is not accessible. It says "This site can't be reached".
It's missing the method type GET, you can do by two options:
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String getString() {
return "Helloworld";
}
Or
#GetMapping("/test")
public String getString() {
return "Helloworld";
}

Index page doesn't work but api works on AWS beanstalk Sprint Boot

I am trying to deploy a spring boot application to aws.
I changed the port to 5000 via application.properties
Now, url/api/hello works fine but url/ doesn't work
I am getting a white label error.
package com.example.package-name;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
#EnableAutoConfiguration
#SpringBootApplication
#ComponentScan()
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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>
<groupId>com.example</groupId>
<artifactId>package-name</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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-web</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>
Can anyone tell me how to fix this?
Is there anything wrong with the main Starter file?
Note: Everything works fine locally, the error page is loading instead
of index page only in AWS
I assume you deploy in aws elastic beanstalk tomcat server, please make sure access via beanstalk URL which will give you after uploading jar/war, generally whitespace error gives you whenever you try to access wrong URL/wrong parameters, if possible provide details with screenshots.

Spring boot jersey error

I generated a project on start.spring.io with the following project dependencies:
Jersey (JAX-RS)
JPA
PostgreSQL
Web
When I try to access localhost:8080/homeroom/webapi/test I get a page with the following error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
I get this error no matter what url I try to access.
In my console info I can see it print Mapping servlet: 'jerseyServlet' to [/webapi/*] so I know my config class is being registered. When I change #ApplicationPath("/webapi") to #ApplicationPath("/"), a GET on localhost:8080/homeroom/test or localhost:8080/homeroom/ returns a blank page instead, with no text or error.
Why can't I access my resource?
This 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>
<groupId>com.homeroomed</groupId>
<artifactId>homeroom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HomeRoom</name>
<description>HomeRoom REST API</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</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>
I'm trying to do a descriptor-less deploy so I have:
#Component
#ApplicationPath("/webapi")
public class MyJerseyConfig extends ResourceConfig{
public MyJerseyConfig() {
// String packageName =TestJerseyResource.class.getPackage().getName();
// this.packages(packageName);
this.register(TestJerseyResource.class);
// System.out.println("The package is:"+packageName);
}
}
And I have the following resource:
#Component
#Path("/test")
public class TestJerseyResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getTest()
{
return "Hi!";
}
}
I'm running the project from:
#SpringBootApplication
public class HomeRoomApplication {
public static void main(String[] args) {
SpringApplication.run(HomeRoomApplication.class, args);
}
}
UPDATE:
So 2 things:
I had to use spring annotations #Controller and #RequestMapping instead of Jersey's #PATH and #GET, and had to GET /webapi/test instead of /homeroom/webapi/test
The reason you can't access your resource from looking at the information you provided is you there is no /homeroom anywhere I saw in your code.
This is a valid URL for your project:
http://localhost:8080/webapi/test
If you wanted homeroom to be in the URL you could change the application path value to homeroom instead of webapi.
Well, quoting from #ApplicationPath JavaDoc:
May only be applied to a subclass of Application.
https://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html
That might be part of the problem...

Categories

Resources