Project not reloading/rebuilding when using Spring Boot Devtools - spring-boot

I followed the steps to enable auto project rebuilding/reloading for IntelliJ:
In my build.gradle file, I have:
compileOnly 'org.springframework.boot:spring-boot-devtools'
From what I can tell, this should be all that is needed.
I'm testing it with the following code:
#Controller
public class HomeController {
#GetMapping("/")
public String hello() {
System.out.println("Huh, interesting... doesn't seem to be reloading at all??");
return "home";
}
}
I'll change the text being printed out, save the file, and I won't see any activity in the 'run' tab in IntelliJ.
If I load '/' in my browser, I see the same text being printed out in the run tab (System.out.println), so it's not picking up any changes.
I've restarted IntelliJ, clicked the 'refresh' icon for gradle, still not reloading when a code change is saved.
I'm not sure if it matters, but I'm on a Mac with an M1 chip.
Edit: I've tried everything, having to click this button each time is going to drive me insane!!!

Related

Springboot read json and create menu link out of it

I need to read a json file with multiple linkname and url then create a menu link mapping out of it.
Menu
linkname1: This is a link1
linkname2: This is a link2
Using org.apache.model.rest.RestDefinition method if possible, any idea?
I have to put
#Controller
#RequestMapping("/xxxx")
On the main class
I have to put
#GetMapping()
public static RestDefinition
But it only run in eclipse and it compile with no error, but if you run, you will have error. I just decided to rewrite it all in spring-boot without RestDefinition and old java programs.

Spring boot, tomcat, rest api 404

I am using Kotlin + Gradle and trying to build a war file to deploy on Tomcat. My application is from the https://start.spring.io plus a simple controller and build the war file using ./gradlew bootWar
#SpringBootApplication
class ServletInitializer : SpringBootServletInitializer() {
override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder {
return application.sources(DemoApplication::class.java)
}
}
#RestController
class TomcatController {
#GetMapping("/hello")
fun sayHello(): Collection<String> {
return IntStream.range(0, 10)
.mapToObj { i: Int -> "Hello number $i" }
.collect(Collectors.toList())
}
}
when I try to access it I get
Type Status Report
Message The requested resource [/demo-0.0.1-SNAPSHOT/hello] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I am super stuck. What am I doing wrong? If I add a html file to the src/main/webapp/index.html it shows up for some reason only the rest api can't be reached.
Spring Boot applications come with a built in Servlet. You are probably already using this feature when launching the application inside your IDE.
This basically means that you can just run your .jar file on any web server and it will be ready to go without setting up an extra tomcat instance.
However, if you want to build a Spring Boot application as a war file and deploy it to an external tomcat, you need to follow some extra steps as explained in this article.
Assuming from what you posted so far: the path that is returned shows another route before your actual controller route "/demo-0.0.1-SNAPSHOT/hello" is this "/demo-0.0.1-SNAPSHOT" the path that your application runs on ? If not it should be included in your controller (assuming you havent set it elsewhere for e.g. in your application.properties).
for e.g. http://localhost:8080/ would be the basepath and either http://localhost:8080/demo-0.0.1-SNAPSHOT/hello or http://localhost:8080/hello would point to your controller. Also your startup logs (for Tomcat and Spring) might give away more about the issue.

Spring Boot: How to serve two different HTML files to two different mappings?

I am pretty new to Spring Boot, and I saw that if I start a Spring Boot project and put an index.html file in the folder src/main/resources/static, then when I open my browser and go to address "localhost:8080", I'll see this file displayed in the browser.
Now, I have another html file (let's call it 'hello.html'), and I also placed it in src/main/resources/static. I wrote the following code:
#Configuration
#Controller
#EnableAutoConfiguration
#ComponentScan
public class TopicController {
#RequestMapping("/hello")
public String hello() {
return "hello.html";
}
}
As you can understand, I want that when I go to localhost:8000/hello, I'll see the display of 'hello.html' file.
However, I get the following error in the console:
javax.servlet.ServletException: Circular view path [hello.html]: would
dispatch back to the current handler URL [/hello.html] again. Check your
ViewResolver setup! (Hint: This may be the result of an unspecified
view, due to default view name generation.)
Do you know what I can do so I can get the hello.html file in the browser for accessing "localhost:8080/hello" ?

Render a template as string in a service in Grails 3 rest-api profile app

I've created a Grails 3.2.1 app with rest-api profile and want to render a GSP template as a String. For this I first added, apply plugin:"org.grails.grails-gsp" in build.gradle then based on the code of ResponseRenderer, I tried following to render my GSP template as:
def viewResolver = applicationContext.getBean(CompositeViewResolver.BEAN_NAME, CompositeViewResolver)
View view = viewResolver.resolveView("/email-templates/signup", null)
But the view object is always null. Then I tried the following as well without success:
[ViewResolverComposite, GroovyMarkupViewResolver, GenericGroovyTemplateViewResolver].each {
def viewResolver = applicationContext.getBean(it)
View view = viewResolver.resolveViewName("/email-templates/signup", null)
println view?.dump()
}
My template is located at grails-app/views/emails-templates/_signup.gsp.
In Grails 2, it was quite simple by injecting groovyPageRenderer bean of type PageRenderer but I think, that bean and class is no longer used in Grails 3.
For Grails 3 app created with web profile, they can always use the groovyPageRenderer as given in the link Grails render view from service?.
And for the app created with rest-api profile, they just need to add the following in build.gradle under dependencies block:
compile "org.grails:grails-plugin-gsp:3.2.1"
And the above link will also work as is.
Just for the sake of description, inject the bean and use the render method:
PageRenderer groovyPageRenderer
def someMethod() {
println groovyPageRenderer.render(template: "/email-templates/signup", model: [foo: "bar"])
}
For Grails 3.3.x, few plugins have been externalized and artefact ids has changed.
Please refer here.
So, to make sure that groovyPageRenderer bean is registered, add
compile "org.grails.plugins:gsp"
to the dependencies block in build.gradle instead of
compile "org.grails:grails-plugin-gsp"
Just to make sure the plugin is initialised successfully in your app, add
logger('grails.plugins', TRACE)
in the logback.groovy file. You should see something like this in the console logs.
Grails plug-in [groovyPages] with version [3.3.x] loaded successfully
For compiling GSP files and adding it to the war/jar package, add
apply plugin:"org.grails.grails-gsp"
to your build.gradle file.
I hope this helps.

Spring Boot - external property not found

My application is trying to externalize all project properties, some ones will be inside my app and another ones will be in a folder somewhere in Windows.
I set up Spring to execute this way: --spring.config.location=file:///C:\Temp\config\application.properties,classpath:application.properties
As you can see, if the same property exists in both sides, application property will be kept (priority order). I noticed for example some properties such as "server.port" can be found if exists outside folder (file://) but if I create one such "common.acronym-name" my project can not find its value.
Why "server.port" has a different behaviour that one create by me? Is there any configuration I need to tell Spring Boot to see this external property in my project?
#Value("${common.acronym-name:}") //Just find it in application classpath
private String acronymEnv;
Thanks!
To simulate this error, just create a class to handle the banner, for example:
#Component
public class ShowBanner {
#Value("${spring.main.show-banner:}")
private String showBanner;
#PostConstruct
public void init() {
System.out.println(showBanner);
}
}
In this code if you set at external property file the property "spring.main.show-banner=false" the banner still shows in console if it is set before server section. When the banner should not appear. Because the property returns empty.
If I move the property after server section the banner disappears, because return false value as expected.
Keep in mind my application.properties project is empty.
By the way, Even running via Eclipse or java console it happens:
java -jar sample-1.2.3.RELEASE.jar --spring.config.location=file:///C:\Temp\config\application.properties,application.properties
Why?
Believe or not, the order of properties makes difference for external properties be found.
If I set this below order where "spring.main.show_banner" is the first one at the top, my properties can not be found. For example:
spring.main.show-banner=false
server.port=9043
server.session-timeout=1800
server.ssl.key-store=file:///C:/Temp/config/localhost.jks
server.ssl.key-store-password=localhost
server.ssl.key-password=localhost
So, If change to below order, everything works fine:
server.port=9043
server.session-timeout=1800
server.ssl.key-store=file:///C:/Temp/config/localhost.jks
server.ssl.key-store-password=localhost
server.ssl.key-password=localhost
spring.main.show-banner=false
Is there any reason for it? Spring Boot needs to have the properties in right order? Seems the "server" section must the first one in the properties.
thanks.

Resources