How to debug a Spring Boot Web Application in STS? - maven

I am new to the Spring Framework and so I started with the Spring Boot projects.
In particular, I took the https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-secure-jdbc project and modified the pom.xml so that it becomes self contained (replaced spring-boot-samples with spring-boot-starter-parent in the parent->artifactId tag, removed properties node, added in repositories and pluginRepositories from http://docs.spring.io/spring-boot/docs/1.2.2.BUILD-SNAPSHOT/reference/htmlsingle/#getting-started-maven-installation).
I successfully built and ran this application outside of STS first with 'mvn spring-boot:run', then imported the Maven project into STS and it builds and runs there successfully.
I am able debug this application by right-clicking on the project folder and choosing Run->Debug As ...->Spring Boot App and the debugger does stop at the breakpoints I've set in the ApplicationSecurity class methods. However, the browser window does not automatically pop-up in STS nor the system browser (which I expect - is this correct?).
When I browse to localhost (port 8080) using the 'Internal Web Browser' within STS/Eclipse the HTML pages render out but the breakpoints that I've set in the #RequestMapping("/") methods do not get hit (I suspect that this is probably due to the fact that the browser is in a separate process to the debugger).
So, how do I debug #RequestMapping methods within STS - this would help me to understand the Spring framework better.
I have limited knowledge of Eclipse/STS, so could there be a problem with my installation?

You don't need to use the embedded browser to debug a server (the browser is a separate process). If your #RequestMapping breakpoints are not hit then I suspect Spring didn't dispatch to those routes for whatever reason (maybe security?).

Related

Spring-Boot Vaadin deploy project on jetty

How to deploy a Spring boot vaadin project on jetty using intellij, the Idea behind that is to refresh the project after each changes without restarting the server.
I don't really understand the people who downvote a question without answering it. however here is the answer:
in Spring framework using spring-dev-tools, you have the option to deploy the project whenever the class path changes( in intellij the class path change event is triggered when we rebuild the project, in eclipse class path change event is triggered as soon as we hit the save file of any class). Server will restart (tomcat or jetty) and load the project (Restart method).
we could overcome this expensive approach using third party plug-ins such as JRebel. JRebel will only deploy the class that has been changed.
consider developing a web app and consider making about 100 runs a day, everytime the server restarts it consumes 3 seconds. do the math

How to run JAX-RS in Maven project of JAVA EE?

This is what I did on NetBeans.
New Project -> Maven -> Web Application
and gave my project name
And, created new RESTFUL Web services from patterns
selected Simple Root Resource
Assigned path "foo"
and hit finish
And now, I tried to access it from browser as
localhost:8084/context/webresources/foo
And, it shows 404 resources not found error.
I checked if it works with non-maven based projects, and it does. Accessing the resource was possible on those but, not on maven based.
I must have missed something.
I also tried to see if it is accessible from netbeans by doing right-click on the RESTful Web Services and clicking TestResource Uri,
It shows error
Unable to open resource url.
the uri of the rest location
Make sure project has been deployed successfully and the server is running.
While both have been done!!

WAS Liberty not serving images, css, js

I have an application which is working fine on Websphere server (as war and Eclipse Project).
Direct deploy on Liberty through WAR is also working fine.
I was trying to deploy it on WAS Liberty through Eclipse project. There are no console errors but once the application loads, none of the following files are getting loaded in the web page: js, gif, css
Because of this the page is looking distorted and most of the functionality is lost.
Surprisingly there are some JSPs in the js folder and these are getting loaded, so looks like the folders are published properly. But for all the mentioned files (js etc), I get following error in the browser console: 500 (Internal Server Error) .
There are no errors and the server log is also clean.
My setup: WAS Liberty 8.5, RHEL 6.5. Eclipse Luna, WDT 8.5.5.2. Project having Eclipse structure, not maven.
I have tried both loose config and 'from workspace' settings
Edit1: I noticed that Spring beans are not getting initialized properly. Getting null pointer on applicationContext.getBean.
OK, found the problem.
As I guessed this was related to spring initialization, though the problem was more code related.
Due to wrong implementation of REST implementation, where the base path was set as root application path (“/”), rest API classes were getting instantiated on application load and were then making calls to code which was instantiating few Spring beans.
But at this point the Spring listeners had not fired, hence the appcontext was empty. Some of the base application object were getting initialized with empty beans and hence spring security context and related classes were failing to serve the application content properly. (Problem is spring related code is created by another team and we just get the jar, hence I am not even able to debug it properly, so I don't know where it was failing exactly)
The thing that makes it Liberty specific is: The same problematic code is working fine on Websphere full profile and Weblogic and even on Liberty if we deploy as WAR. Not sure what classloading difference is causing difference in behaviour.
Thanks to all who took time and efforts for replying.
I am facing the same issue using open liberty and Eclipse Krazo when the base path is
application path (“/”)
so i have changed the path as shown below
package io.openliberty.sample;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("")
public class ConfigApplication extends Application {
}
Now the CSS and imgs are loading with no issues.

Eclipse Java EE project and Spring : classnotfoundexception

I am trying to build an project in Eclipse (actually I'm using RAD, so basically eclipse, and when I say 'Java EE Project' I mean an 'Enterprise Application Project').
My Enterprise Application Project (the 'EAR' project) has two module projects :
- service
- web
The service project has some stuff in it, all wired up using Spring.
The web project has its own stuff in it, all wired up using Spring. The UI stuff in the web project needs to use the stuff in the service project.
Both projects are included in the EAR project as modules.
The web project lists the 'service' project as a dependent project in the build path, it's checked off for export, and also has it listed as a EE Module Dependency.
I'm having a really hard time to get this working though:
The spring context in the web project is of course what gets loaded when the application is deployed, and it imports the spring config I need from the service project. This seems to be working fine.
When spring tries to instantiate a bean it throws a ClassNotFoundException. On the very first bean.
I tried simply copying the spring config from my service context and pasting it into my web context, but I got the same ClassNotFoundException.
I have tried instantiating an object of that type (the class that spring says cannot be found) in the java controller class in the web project, and it is successful, both at compile time (no compile errors) and at runtime (no exceptions).
So the classes from my service project are not available on the classpath when spring tries to use them.
Any ideas what's going on here and/or what I might be able to do about it?
There is a class loader policy that you should use ParentClass First . That will be managed either through Application.xml or through web.xml . You need to check your xml's then try.
It's a class loader issue.
Since you're using Spring, I'll assume that you don't have EJBs. If that's the case, why do you need an EAR? Deploy the whole thing as a web project, in a single WAR.
Put all your .class and Spring configuration .xml files in WEB-INF/classes. Load the configuration using org.springframework.web.context.ContextLoaderListener.
I seem to have fixed this - I'm not sure exactly what the problem was but there must have been a small typo in my spring config. I decided to just start fresh with a new spring config and when I started building the new one back up things were working fine. There must have been a problem with the old one.
Thanks for the suggestions though.
Unfortunately we're not always able to change project structure. We're working on structures other people have put in place.
I looked into the ParentClassFirst vs ParentClassLast setting - it seems on websphere the ParentClassFirst setting is the default if you don't specify anything, so I'm leaving it without specification to get that functionality.
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/crun_classload.html

JSP debugging in IntelliJ IDEA

Does anyone know how to debug JSP in IntelliJ IDEA?
When I set breakpoint in my JSP files, those breakpoints never seem to take effect. The debugger never hits them. IDEA seems to think that the breakpoints are valid. I do see a red dot placed to the left of the line where I place my breakpoint.
I read in IntelliJ forum in this post that JSP files need to be under web-inf for debugging to work.
But then I also read that JSP files placed under web-inf won't be directly accessible by the user.
I am not sure who's really right.
For JSP debugging in Intellij there are some configurations that must be in order. The fact that Intellij always allows you to add a breakpoint on a JSP line does not necessarily imply that you’ve configured JSP debugging. In the following I refer to Intellij 8 configuration, w.r.t. previous versions you will need to do similar operations as the concepts are the same.
In order to enable JSP debugging you must do two steps: set a web application configuration in your project and add a web application server configuration.
Web application Configuration: in order to have JSP debugging, you must have a “web” facet in your project structure, pointing to the correct web.xml file. Depending on the kind of web application structure you are using, the facet may be detected automatically by Intellij (go anyway to check what it has done) or you may have to add it manually. Remember in the “Java EE build settings” tab to set as anable “Create web facet exploded directory”; if you don’t want duplications, a trick is just to enable it and point to your already existing directory.
(Web) Application server: Go to “edit configurations”, there you have to add to configurations an application server, not launch the web server as an application like any other. In this way Intellij will be able to intercept JSP calls. In the list of application servers, you should have the default one, Tomcat. Be sure to have a local Tomcat installation before you do this, and point to that when adding the web application server. The last trick is going to the “Deployment” tab and selecting as “Deployment source” the same facet that you configured in the previous step.
The same configuration works if you want to use another web application server, I tested it with the latest Caucho Resin releases and debugging works fine (it didn’t with the previous Intellij and Resin combinations).
If you don’t see Tomcat in the list of available application servers to add, check the plugins in the general Intellij settings pane: in the latest releases, more and more functionality has become “pluggable”, and even very basic functions may be disabled; this plugin is called “Tomcat integration”.
Finally, it is surely not true that JSP files need to be under WEB-INF to be under debugging.
For remote JSP debugging (which also applies to localhost) you'll need to install the JSR45 support plugin. Please note this feature is only supported in the Ultimate edition of IntelliJ, not the community edition.
Go to Preferences > Plugins, search for the JSR45 plugin, and
enable it.
Create a run configuration: Run > Run Configuration > click the + button, and pick JSR45 Compatible Server, and then in the dialog that opens, select Remote, and set server host and port. Setting Application Server: Generic should work fine.
Make sure you set the correct port in Startup/Configuration > Debug.
Open the module settings (F3 on the project folder), and add a Web Facet under Facets, and under Web Resource Directories specify your JSP root folder.
Click the Configuration... button, and select the folders with the beans, classes and libraries that your JSPs depend on.
Now JSP breakpoints should work, provided that you started your server with the proper debug arguments.
If you have a maven project with auto-import enabled then you might want to disable auto-import because every time the auto-import is triggered your library settings will be reset.
Also see:
https://www.jetbrains.com/help/idea/run-debug-configuration-jsr45-compatible-server.html
Anyway, you need to launch the Tomcat in IDEA, not from a remote Tomcat.
Please make sure, that in you tomcat's conf/web.xml suppressSmap is not enabled as support of JSR45 is required by IntelliJ's debugger.
It should look like this:
<init-param>
<param-name>suppressSmap</param-name>
<param-value>false</param-value>
</init-param>
From https://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html
suppressSmap - Should the generation of SMAP info for JSR45 debugging be suppressed? true or false, default false.
If you are using the Intellij debugger you can get the value of an individual attribute by putting a breakpoint inside JSP and evaluating the expression this.jspContext.request.getAttribute("attributeName").
Note that this may return a Java Object type, and you may have to cast it to the correct type. Also if you launch a remote Tomcat the IDEA won't hit any breakpoints, so you need to launch the Tomcat in debug mode from inside the IDEA.
For the second part of your question ("jsp files placed under web-inf won't be directly accessible by user") that is correct. To allow users to access JSP files in the WEB-INF folder servlet and servlet-mapping entries need to be made in the web.xml file for each JSP page.

Resources