Java Web Application use Sprint Boot Client will appear the HTTP Status 500 error

Answered

I create a Sprint Boot project and Sprint Boot Client project (console mode). These connect with no problem. I want to create a Java Web Application with the Spring Boot pom.xml and reference the code of this web(as below) to create three buttons.

How do I call a specific Java method on click/submit event of specific button in JSP?

My button event code as below. 

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

public class MyClass {

public MyClass()
{

}

public void method1()
{
System.out.println("method1");
RestTemplate restTemplate = new RestTemplate();
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
headers.setAccept(Collections.singletonList( org.springframework.http.MediaType.APPLICATION_JSON));
String resourceURL = "http://192.168.56.4:8080/run?name=/home/myuser/test";
org.springframework.http.HttpEntity<String> entity = new org.springframework.http.HttpEntity<String>(headers);
org.springframework.http.ResponseEntity<String> response2 = restTemplate.exchange(resourceURL, org.springframework.http.HttpMethod.GET,entity,String.class);

if (response2.getStatusCode() == org.springframework.http.HttpStatus.OK)
{
System.out.println("Hello World");
System.out.println(response2);
System.out.println(response2.getBody());
}
else
{
System.out.println("Error");
}
}

public void method2() {
//some code
System.out.println("method2");
}

public void method3() {
//some code
System.out.println("method3");
}
}

 

When I click one of the buttons, the web browser will appear the HTTP Status 500 - Internal Server Error as below.

HTTP Status 500 – Internal Server Error
Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NoClassDefFoundError: org/springframework/util/MultiValueMap
com.mycompany.jsp.MyServlet.doPost(MyServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.ClassNotFoundException: org.springframework.util.MultiValueMap
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1328)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
com.mycompany.jsp.MyServlet.doPost(MyServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.13

 

If I comment the Spring Boot code in MyClass' method1, I click any button and there is no error appear.

Does somebody know how to integrate Spring Boot to Java Web Application?

 

0
2 comments
Avatar
Permanently deleted user

My pom.xml content is 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>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</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.1.0.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</artifactId>
</dependency>

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

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
0

Hello,

Seems needed library is missing from the classpath. Is it possible to provide project example for investigation?

 

0

Please sign in to leave a comment.