Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij Run Configuration Spring Boot vs Maven Issues

I'm trying to run my Spring Boot project with Intellij IDEA. This project is of type Spring MVC, meaning that it has JSP files in path main/webapp/WEB-INF/jsp.

My application.properties has these settings:

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp

When I'm running the project in Intellij as a "Spring Boot Run Configuration" the server can't identify the path of the JSP files. This is the message i get:

enter image description here z

If I run the project with "Maven Run Configuration" with the command spring-boot:run everything is working fine.

I have no idea why this is happening. The code and settings of the project are the same with each Run Configuration.

You can try this project (not my project) to understand what I mean https://github.com/mariuszs/spring-boot-web-jsp-example.

My project is behiving the same. I should note that when the project is running with Spring Boot Run Configuration I do see in the console that the controllers are mapped correctly.

2017-06-28 08:29:13.906  INFO 10308 --- [           main] o.s.w.s.h.SimpleUrlHandlerMapping        : Mapped URL path [/login] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]

2017-06-28 08:29:13.835  INFO 10308 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.intuit.mintbills.controllers.generic.LoginController.getLoginPage()
like image 481
OmriYaHoo Avatar asked Jun 28 '17 05:06

OmriYaHoo


People also ask

Is IntelliJ good for Spring Boot?

IntelliJ is really a powerful editor and provides first-class support to create and run Spring Boot based web applications.

How do I run an existing Spring Boot application in IntelliJ?

Configure the application update policy From the main menu, select Run | Edit Configurations. Select the necessary Spring Boot run configuration to open its settings. Click Modify options.


2 Answers

I solved this problem by setting the working directory to $MODULE_DIR$ in the run configuration.

like image 102
Muzammil Avatar answered Sep 28 '22 05:09

Muzammil


This is a problem with IntelliJ and Spring boot. You should check the following things:

Web facet

Make sure that you have the Web facet enabled. The sample project does not have a web.xml file, and due to that, IntelliJ doesn't pick up src/main/webapp. You can add a dummy web.xml file to src/main/webapp/WEB-INF and reimport the project, or you can manually add the web facet and configure it properly. If the web facet is configured correctly, you should see a blue circle in the webapp folder.

An example of a dummy web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

And this is the "blue dot" in the src/main/webapp folder:

project structure

Provided dependencies

Another issue with IntelliJ is that the provided dependencies are not properly picked up. You can usually fix this by opening Project Structure..., by opening Modules and selecting the Dependencies tab. In here you can override the scopes of your dependencies, so in your case I would suggest configuring both tomcat-embed-jasper and ecj to Compile.

dependency configuration

I haven't done this recently, but it's possible that this will be overriden everytime you change your pom.xml. In that case I would suggest overriding the <scope> in you pom.xml:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

If you're planning to deploy your application on a web container (eg. an external Tomcat), you may want to work with profiles, otherwise this could lead to conflicts since these dependencies are provided by the web containe.

JSP limitations

It's also recommended to change you packaging to WAR, since there are a few known limitations by using JSPs with JAR files. Quoting the documentation:

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 Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work because of a hard coded file pattern in Tomcat.
  • With Jetty it should work if you use war packaging, i.e. an executable war will work, and will also be deployable to any standard container.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page won’t override the default view for error handling, custom error pages should be used instead.
like image 31
g00glen00b Avatar answered Sep 28 '22 05:09

g00glen00b