Spring boot: 404 error when calling JSP using controller - java

I'm getting the following error when running my project using Spring Tool Suite,
But in case my problem is I have already added the appropriate dependencies to pom.XML file. So what could be the problem?
My pom.XML file dependencies as follows,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
My controller ApplicationController.java as follows,
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class ApplicationController {
#RequestMapping("/")
public String Welcome() {
return "welcomepage";
}
}
My vives are in src/main/webapp/WEB-INF/view/welcomepage.jsp you can look the tree view below,
And I have already changed the application.properties file as well. But still, I can't understand what is wrong.
My application.properties file as follows,
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
I just print hello in My welcomepage.jsp,
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>

Looks like you was very close to the working application. The main issue in your code is in <scope>provided</scope> for your Jasper dependency.
And also looks like you are running your code from eclipse IDE through the main method.
Long story short:
If you would like to run your application through the main method in MyApplication.java then just remove scope provided for the Jasper.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Or you can run your application exactly in that state like you have right now from the console:
mvn clean spring-boot:run
But I suggest to remove this scope so you could be able to run your code from IDE and from console. In addition to that looks like spring-boot-starter-tomcat dependency is redundant (it must be available within spring-boot-starter-web). In a nutshell please try to use following pom file:
<?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.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
</project>
Hope my answer will help you.

You may also need to add this in pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
UPDATE 1:
JSP Limitation
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use war packaging. An
executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view
for error handling. Custom error pages should be used instead.
Scope
compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
Also, Try to change the Following in tomcat-embed-jasper
Remove <scope>provided</scope> OR change the scope to compile <scope>compile</scope>
JSP Limitations
Spring Boot JSP 404

I was able to generate a jar from my application and then run it with java -jar myapp.jar But I only managed to run this jar with the version below spring-boot-starter-parent:
MyApp/pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
I researched in:
Spring Boot JSP 404

Related

Problems accessing REST endpoints from Springboot application deployed on Tomcat

I am developing a springboot REST application with Eclipse, but I have a problem deploying it to Tomcat 10.0.22. When I run my application from the IDE the endpoints are working fine, they returns me the correct JSONs but when I try to access them from my server they throw a 404 error.
I am packaging it as war. I have created the project from https://start.spring.io/ setting Spring web dependency, java 18 and packaging war. I imported it to eclipse as an existing maven project.
To be sure that I deployed it correctly I had created a jsp file and load it as index.jsp and it is showing correctly on the deployed server and the IDE.
I have tried to restart the proyect, change the java version in both, export the proyect as jar, to deploy it on another tomcat version, to extend SpringBootServletInitializer from the main class and override the confirm method, without the method implemented and without extending it from anywhere.
I don't understand why when I deploy it to the Tomcat server from the Tomcat manager the endpoint calls are throwing a 404 error. I think that is possible that Tomcat is not loading Springboot libraries, because I do not see the console Spring initializing message when I start the server from console, but I do not know how to solve this or if it is what is happening. Can anyone help me please.
This is my main class, TestApplication.java:
package com.testing.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
This is my servlet initializer application, ServletInitializer.java:
package com.testing.test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TestApplication.class);
}
}
This is my controller, UserController.java:
package com.testing.test.controllers;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class UserController {
#PostMapping("/test/User")
public String user(#RequestParam(name="id", defaultValue = "NO_ID_RECEIVED") String id) {
return String.format("The user ID is: %s",id);
}
}
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 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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testing</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>18</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>
The mentioned index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Let's see if it deploys</h2>
</body>
</html>
EDIT: Because I have had a questions about the structure below this I am appending a picture of it.
Project folder structure
Just to be sure have you put your JSP files under web-content and not under WEB-INF because in Eclipse the files are not accessed there by the server.
And one more thing try to check server location and see whether Tomcat is installed in the correct server location and it is showing in your Eclipse,if not try changing your server location to the location where Tomcat is installed and restart your server.

Simple Spring Boot Project is giving 404 Error

I am learning Spring Boot but I am not able to execute the basic "Hello World" program. I have built the basic project using https://start.spring.io/ and updated the code as per below code snippets:
Spring Boot Main Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan(basePackages = "com.rohit.springboot.practice.selfspringboot")
public class SelfSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SelfSpringBootApplication.class, args);
}
}
Controller Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ThisWorldSpringContainer {
#RequestMapping(method = RequestMethod.GET, value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
POM.xml file:
<?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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rohit.springboot.practice</groupId>
<artifactId>self-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>self-spring-boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<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>
Error:
Message: The requested resource [/hello-world] is not available
I know this question has been asked by multiple people but i have gone through those answers and tried those given answers but didn't work.
Technologies used are Java 8, Tomcat 9, Spring Boot 2.6.6 etc.
Kindly help.
Since you don't have the Tomcat dependency, I'm implying that you want your application to run in a standalone Tomcat instance. Also, given your comment, I believe you're working on either Eclipse or Spring Tools Suite (Which is the same, really).
With that, it seems your problem is the lack of a packaging method. Tomcat needs the code its going to run in a package. This package is a zip-like file with .war extension. Maven, the program that handles the dependencies of your project according to the pom.xml file, can package it in either war of jar format. Since you are running it in an external Tomcat, you want the war extension. So please, add the following line to your pom.xml file. It has to be in the same level as <name>:
<packaging>war</packaging>
Then, update your project (Alt + F5, check your project and hit OK) then try running your project again. You should see an stylish "SPRING" output in your console.
Now, since you are running this externally, you need to specify the name of the project, so you should call your endpoint with
http://localhost:8080/<your_project_name_here>/hello-world
I advise you to check this new change in Spring Boot 2.6 :
PathPattern Based Path Matching Strategy for Spring MVC
In this release, the default strategy for matching request paths against registered Spring MVC handler mappings has changed from AntPathMatcher to PathPatternParser.
If you need to switch the default strategy back to AntPathMatcher, you can set the property spring.mvc.pathmatch.matching-strategy to ant-path-matcher as in the line below to add in the file application.properties :
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
I encountered the same issue of HTTP 404 error when upgrading to Spring Boot 2.6 and adding this property solved my problem and should solve yours.
I have modified your code as below:
#RequestMapping
#RestController
public class ThisWorldSpringContainer {
#GetMapping(value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
I thinking you are missing
#Requestmapping
in classlevel

How do I import jwt.io Java library to JMeter

I am trying to import the jjwt Java library into JMeter but I get the following error;
ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223
Sampler, message: javax.script.ScriptException:
groovy.lang.MissingPropertyException: No such property: io for class:
Script4
I am attempting to use the jjwt Java library https://github.com/jwtk/jjwt ... which can be found on the https://jwt.io/ website. I've added the files to the JMeter classpath and used a JSR223 sampler to write a script in Groovy... but it doesn't seem to accept it. I'm guessing that this is because it's expecting .jar files but the library is .java files, maybe?
Any theories on how to import this jjwt library would be greatly appreciated.
There is another post here; How to generate JWT token on JMETER using a RSA 256 private key Required library or jar file? that discusses this method and shows that it should work.
.java files are "text" files, they need to be compiled into .class files before you can run them in JVM. You can download pre-built .jar from Maven Central (make sure to fetch all the dependencies as well), or if you prefer the "hard" way:
Install Apache Maven
Create a file called pom.xml somewhere with the following content
<?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>org.example</groupId>
<artifactId>jwt</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Open that folder in the terminal application
Execute mvn dependency:copy-dependencies command
Copy everything from target/dependency folder to "lib" folder of your JMeter installation (or other location in JMeter Classpath)
Restart JMeter
Add JSR223 Sampler to your Test Plan and put your Groovy code using the jjwt library functions there.

How to make thymeleaf spring security namespace available?

I've got a Spring Boot app. From what I understand a boot app will only need the dependency in the pom and all is great. Unfortunately, that's not the case and even when I overcomplicate my configuration it still doesn't work - I can't use the sec namespace in my pages.
In my page the first issue is the namespace URI:
I've tried every option available in the Intellij fix menu and can't get it.
I suppose the result of that issue is the fact that I can't use the sec namespace anywhere. The pictured example may indeed be an invalid use but I've used <div> as well which is straight from the Thymeleaf examples:
Many of the answers here and other sources are relying on xml configuration as well, which is of no use. Still, I've made Java-based beans based on those xml examples with no luck.
What steps are required to use spring security and thymeleaf integration in a spring boot app using only Java based configuration (if that)?
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
...
</dependencies>
I have encountered the same issue and for me it helped to define schema locations like this:
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org http://www.thymeleaf.org
http://www.ultraq.net.nz/thymeleaf/layout http://www.ultraq.net.nz/thymeleaf/layout
http://www.thymeleaf.org/thymeleaf-extras-springsecurity4 http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
...
This includes also additional dialects for layout and spring security 4 which you can remove if you are not using them.

How can I import Spring Framework into Eclipse?

I am having trouble importing spring framework into eclipse. I have downloaded spring but am unable to import it. Can anyone help or direct me to a web page that can do so?
You can either follow this tutorial:
http://www.tutorialspoint.com/spring/spring_hello_world_example.htm
or use the spring eclipse plugin (you can find it in eclipse under Help/eclipse marketplace and search for it)
or you can use a tool to manage your dependencies like maven.
It wouldn't be a bad idea to get familiar with a dependency manager like Maven. For use in Eclipse there is a nice plugin M2Eclipse. The pom.xml file is what you use to specify your dependencies, and then when you build with Maven it resolves everything for you and downloads automatically anything it needs. This is the example pom.xml file from the Maven installation instructions:
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The dependency for the core Spring Framework is
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
And generally you can find the pom.xml dependency specifications at the Maven Repository. Might seem a little overdone for simply getting Spring into Eclipse, but once you have it set up it is then a snap to add more dependencies down the road, all you do is add the appropriate item to the pom.xml file.

Categories