spring boot error 404 while starting jar - spring

I’m trying to run a spring boot application. I downloaded a code from a tutorial so I know the code works. When I run my jar file, it looks like the applications I running on port 8080 but still I get 404 for any URL, I never got the spring white label page. I checked that I don’t have anything else running on port 8080, and the server I up and running.
I have no clue why my applications are not responding.
running the jar
error 404
package com.virtualpairprogrammers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by masn on 2017-04-01.
*/
#RestController
public class HelloController {
#RequestMapping("/hello")
public String sayHello(){
return "Hello";
}
}
enter code here
package com.virtualpairprogrammers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class FleetmanApplication {
public static void main(String[] args) {
SpringApplication.run(FleetmanApplication.class, args);
}
}
enter code here
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.virtualpairprogrammers</groupId>
<artifactId>fleetman</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fleetman</name>
<description>VPP´s Fleet Management Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>

If you run the command java -jar YourAppName.jar you must be located inside the root folder (fleetman) and NOT INSIDE the target folder where the .jar is located.
This is a common mistake so run the command inside the fleetman folder.
You might want to update the pom.xml with this
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>

Just try like this and it will work:
#RestController
public class HelloController {
#RequestMapping(path = "/hello", method = RequestMethod.GET)
public String hello() {
return "hello";
}
}
Here's the screenshot I've taken from my computer:

Related

Application execution failed and not getting expected outcome

I am developing a simple spring boot application. I am creating a LoginController class and trying to access the API which is present in the LoginController class. I am able to execute the program, but not getting the expected output while I am trying to access the API.
Login Controller
package com.in28minutes.springboot.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class LoginController {
#GetMapping("/login")
public String loginMessage() {
return "Welcome to Springboot Application";
}
}
Bootstraping Application
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.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 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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot.web</groupId>
<artifactId>spring-boot-web-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-web-example</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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 run the project several times but don't get the output as expected. Can anyone help me out?
#SpringBootApplication annotation scans the current package and the sub-packages to look for #Component. In your case, LoginController is not in the package, not a sub-package of SpringBootWebExampleApplication.
Either move com.in28minutes.springboot.web.controller in com.in28minutes.springboot.web.springbootwebexample.controller
Or, use #SpringBootApplication(scanBasePackages = "your.package") on your SpringBootWebExampleApplication
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = "com.in28minutes.springboot")
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.class, args);
}
}
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#ComponentScan("com.in28minutes.springboot")
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.class, args);
}
}

Spring boot: Getting 404 status always

I tried to setup Spring boot basic example in my machine. BUt after setup, i am getting 404 status code returned.
I tried so many solutions provided on stack overflow. But none of them resolved my problem. Please help me, TIA.
I tried sub package structure to main mathod present package.
I tried adding scanBasePackages to #SpringBootApplication. Still no luck.
my code: Main Class . I tried by using without and with scanBasePackages.
package com.online.sbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = {"com.online.sbo"})
public class OnlineSboApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineSboApplication.class, args);
}
}
My controller: In server logs its not showing that this controller is registerd.
package com.online.sbo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController("/home")
public class HomeController {
#GetMapping(value="/render", produces =
MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public String renderHomre() {
return "gfvbytfgh";
}
}
My Pom.xml: I have not added any security in my project. I have created this project from start.spring.iosite.
<?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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.online.sbo</groupId>
<artifactId>OnlineSbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>OnlineSbo</name>
<description>Odisha Bank</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-tomcat</artifactId>
<scope>provided</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>
Application.properties:- There is No entry in this file.
You use #RestController attribute wrong.
This should work:
#RestController
#RequestMapping("/home")
public class HomeController {
#GetMapping(value="/render", produces =
MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public String renderHomre() {
return "gfvbytfgh";
}
}
ps: In your example localhost:8080/render should work, too.

Unable to run Spring Boot simple REST service

I'm trying to run a basic Spring Boot application created with the Spring Initializr. I see the application is pretty much the same of many examples available on the Web but I get a '404' page not found:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
#RestController
public class Controller {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
return "it works!";
}
}
This is the main class:
package com.example.springtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The pom.xml includes just the web starter:
<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.example</groupId>
<artifactId>spring-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</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-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>
The execution:
$ curl http://localhost:8080
{"timestamp":"2018-12-11T09:18:59.236+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
Do I miss any starter to get it working?
Thanks
use #ComponentScan() annotation in the main method class and pass the path to the rest service component scan as a parameter.
because by default it only include the service which is in the package and sub package of the main class . so for adding other path service you have to pass the path in component scan annotation.

Spring Boot Not Finding Any Controllers

Hello i've tried to create a simple controller and put it in the same package with the main class but it looks like it's not finding it.Basically, when i run the app it's not mapping the endpoint.
I just can't figure out what is wrong with it.
Here is the code:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestProject1Application {
public static void main(String[] args) {
SpringApplication.run(TestProject1Application.class, args);
}
}
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HomeController {
#RequestMapping(value="/hello", method = RequestMethod.GET)
public String sayHello() {
return "HELLO";
}
}
Project Structure and Console:
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.tsv</groupId>
<artifactId>TestProject-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestProject-1</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>
Although in your log, I do not see 'ServletRegistrationBean' getting initialized, I find no issue with your configuration. Try execute the Spring boot app again & you should be able to invoke the HomeController via 'localhost:8080/hello'.

SpringBoot not deploying on weblogic

I am trying to deploy simple springboot app on weblogic. but see some error.
my java classes are
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.javainuse</groupId>
<artifactId>SpringBootHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootHelloWorld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.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>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This is my SpringBootHelloWorldApplication.java class
package com.javainuse;
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.WebApplicationInitializer;
#SpringBootApplication
public class SpringBootHelloWorldApplication extends SpringBootServletInitializer implements WebApplicationInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
and this is my TestController class.
package com.javainuse.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
I get below error
Caused By: com.bea.xml.XmlException: failed to load java type corresponding to e=web-fragment#http://xmlns.jcp.org/xml/ns/javaee
at com.bea.staxb.runtime.internal.UnmarshalResult.getPojoBindingType(UnmarshalResult.java:371)
at com.bea.staxb.runtime.internal.UnmarshalResult.determineTypeForGlobalElement(UnmarshalResult.java:326)
at com.bea.staxb.runtime.internal.UnmarshalResult.determineTypeForGlobalElement(UnmarshalResult.java:336)
at com.bea.staxb.runtime.internal.UnmarshalResult.determineRootType(UnmarshalResult.java:317)
at com.bea.staxb.runtime.internal.UnmarshalResult.unmarshalDocument(UnmarshalResult.java:168)
Truncated. see log file for complete stacktrace
I am using weblogic 12 with java8.
Please assist with this issue since I tried all the options already provided and it is not working. I get this error when server tries to deploy this application.

Resources