Spring Boot now displaying the index.jsp page - spring-boot

I am a newbie with Spring boot.
I am trying to return my index page in the controller but I am not able to. Its just returning the word index in the output. Here's my code:
Controller class:
#RestController
public class IMEIController {
#Autowired
private IValidateIMEI iValidateIMEI;
#GetMapping("/")
public String home() {
return "index";
}
}
This is my structure
Application.properties file
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Output:

The issue is with #RestController. Use #Controller annotation instead.
#RestController is use to create RESTful web services.
Here some articles if you need to know more.
The Spring #Controller and #RestController Annotations

Spring boot does not work directly with JSPs. You will have to add tomcat Jasper embedded dependency to get it working.
https://www.baeldung.com/spring-boot-jsp
Also change #RestController to #controller.
And if you want to develop Rest + JSP in the same controller then use #Controller on class level and add #ResponseBody for the rest API methods which will return response in the body instead of returning a JSP page.

Just add your jsp folder in build path of the project:-
Right click on the project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > Add your views folder > Apply and Close

Related

Is #RequestMapping annotation a replacement of DWR?

I'm new to the spring-boot framework and a little bit confused.
Previously we use DWR to convert JAVA method in server's war package to javascript function. In Spring boot is the annotation #RequestMapping do the same? Thx.
No, the #RequestMapping annotation is not related to Direct Web Remoting. The annotation is used in the controller layer of Spring web applications to map requests onto methods.
A typical example for a RequestMapping looks as follows:
#RequestMapping(method = RequestMethod.GET, path = "/users")
public ResponseEntity<List<Post>> getPosts() {
[...]
}
This will map incoming GET requests to path /users to this method.

How to add rest controller to spring boot library, and use it in another application?

I am working on a library xyz.jar that needs to add a UI page with mappings like this one:
#RestController
public class LibCtrl {
#EventListener(ApplicationReadyEvent.class)
#RequestMapping("/updateDomainList")
String updateDomainList() {
return "we can call a controller from another jar like this";
}
}
This then needs to be called in my main springboot application, myMainApplication.war, so when I call
http://localhost/myMainApplication/updateDomainList
I should see
we can call controller from another jar like this
on the browser.
How can achieve this? #Component also did not work for me. Once it begins to work, would #Autowired to JdbcTemplate also work?
It was a simple fix. #ComponentScan allows for multiple packages to be scanned. This made is possible for me to add my Library Packages to be managed by Spring. Just add the following to your application class.
#ComponentScan({"my.mainapplication.package","my.library.package"})

Spring Boot 404 Error with Simple Controller

I am trying to make a simple Spring Boot Application with the simple controller (Restful controller) where the function is returning the String that is to be printed on the Web page. But I am facing a problem where I am getting 404 error every time. In my views, I am setting up the application right. I have just used spring-boot-starter-web as the dependency.
Application file:
Any help would be appreciated.
And I know #SpringBootApplication automatically take all the annotation.
Either your controller class needs to be in or sub package of SpringBoot Application class or the package in which controller class exists needs to be scanned explicitly using #ComponentScan to resolve this
Can you show me the structure of your project?, I believe the problem Spring is not finding your userController, #SpringBooApplication use the #ComponentScan how is in charge to search for all the spring annotations un your code, so I believe is something
Need to add #ComponenScan the resquest to http:localhost:8080/api should return "Hello" if you need to add a path http:localhost:8080/user/api do as follow:
#RestController
#RequestMapping(value = "/user")

Serving single html page with Spring Boot

I'm developing a simple Spring Boot app that uses Spring Boot Web. I placed my index.html into the templates subdirectory of the resources directory as per the Spring Boot docs. I've defined inside the application.properties file:
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
My WebMvcConfigurerAdapter looks like this:
#Configuration
#EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter{
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
super.addViewControllers(registry);
}
}
My problem is I cant seem to serve index.html at http://localhost:8080.
I get the following error:
"javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'".
How can I fix this without the use of a controller?
Kind regards,
Dave
In order to serve static content from Spring Boot application you just need to place them in src/main/resources/static - no other configuration needed.
put index.html to src/main/resources/static/index.html
delete properties from application.properties
delete addViewControllers method and #EnableWebMvc annotation - you can actually delete whole class
access index by going to http://localhost:8080/

Plugin System in Spring Boot for modular applications

I looking for dynamically loading jar in spring boot after compiling, for example I will put jars in some folder and when spring boot is started, all jars from this folder will be injected into spring boot app. I don't know how can I do this with spring boot, and if You know can help me with this, with some example.
I need this jars to have #Service, #Controller as this will be module (plugin), with adding capabilities to my spring boot app.
Is possible to do this with spring boot, and if it is possible, please provide me with some sample code.
Thanks in advance.
UPDATE:
I found something https://www.youtube.com/watch?v=F-sw2pFdcDw https://code.google.com/p/jspf/
UPDATE 2: I can't get #Controller bean from plugin jar registered in Spring Boot
Have a look at FlexiCore, an open-source framework that brings modularity to spring boot utilizing plugins(jars) loaded at runtime See wizzdi and FlexiCore.
for example FlexiCore allows you to create a project ( compiled into a seperate jar from your main application) that contains a spring bean as follows:
#Component
#Extension
public class HelloWorldService implements ServicePlugin{
public String hello() {
return "Hello World!";
}
}
it will be automatically be loaded once placed inside the designated plugins folder, it basically allows a full support for most(all) of spring boot features , so for example you can add a RestController bean to your jar as well , FlexiCore will automatically load that bean allowing you to call the controller as if it was in your main application jar:
#RestController
#Extension
public class TestEntityController implements Plugin {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#Autowired
private TestEntityService testEntityService;
#PostMapping("/createTestEntity")
public TestEntity createTestEntity(#RequestParam(name="name", required=false, defaultValue="Stranger") String name) {
return testEntityService.createTestEntity(name);
}
#GetMapping("{id}")
public TestEntity getTestEntity(#PathVariable("id")String id) {
return testEntityService.getTestEntity(id);
}
}
Disclaimer: I am the CTO of wizzdi, the company powering FlexiCore.
One option is definitely to just use broad #ComponentScan. If you add new jar to classpath the annotated classes from that jar will get discovered via #ComponentScan, #Controllers will get mapped etc.
The XML equivalent here would be placing xml configuration files somewhere to your classpath (META-INF folder being obvious choice) and import them all using wildcard. The idea is the same. If the plugin jar file is on classpath you will get the xml file imported and the beans (controllers, ...) will get loaded.
There are drawbacks to this approach like the modules not being isolated but its definitely option for simpler applications.
You can find a sample spring boot web project here.
By dynamically loading jars I assume you want to add dependencies to your project. For this you can update pom.xml of the sample project and put your dependencies here.

Resources