I have a total simple Spring Boot app written in Kotlin.
#SpringBootApplication
class DemoApplication
fun main(args: Array<String>)
{
SpringApplication.run(DemoApplication::class.java, *args)
}
#Controller
class HomeController
{
#GetMapping("/")
fun home() = "index.html"
}
In the resource/public folder I have index.html.
However, when I open my browser and typelocalhost:8080/ I get the error java.lang.IllegalStateException: Could not resolve view with name 'index.html' in the running Spring Boot console.
I just want the most minimal code possbile to serve index.html. Shouldnt be that hard? ;)
NOTE: I am using Spring-Web-Reactive
Related
I am following the basic Spring Boot application steps to setup the basic Spring Boot app. Have created REST controller as required. After running the application, however, I get this text on localhost:8080- "Whitelabel Error Page
This application has no explicit mapping for /error,
so you are seeing this as a fallback.
Thursday Nov 07 01:41:42 IST 2019
There was an unexpected error (type=Not Found, status=404).
No message available "
Tried adding another callback method apart from the '/' mapping, but it gives the same error.
Controller code:
#RestController
public class TestController {
#RequestMapping(value = "/")
public String home() {
return "Spring boot is working!";
}
#RequestMapping("/testing")
public String testing() {
return "Spring boot is working!";
}
Explicit #ComponentScan doesn't require if your Application class annotated with #SpringBootApplication is in root and others are sub packages of it.
Try putting in below class level annotation in your main class:
#ComponentScan(basePackages = {(name of your controller package)}
I am attaching the project structure.When I run as Spring boot application it shows white label errror. Since controller is in base package there is no need of component scan too.Can some one say whats the mistake.
Controller:
#Controller
public class HelloWorldController
{
#RequestMapping("/greeting")
public String greeting()
{
return "Hello World";
}
}
Use #RestController instead of #Controller if want to test you response output. Spring will treat this as rest controller. else, add #ResponseBody to your controller
I used #RequestMapping or #GetMapping for routing.
But, it doesn't work. It makes whitelabel error page.
(spring boot)
my Application.java
#RestController
#SpringBootApplication
public class SmDemoApplication {
#RequestMapping("/")
String home() {
return "Hello World! smDemo 111";
}
public static void main(String[] args) {
SpringApplication.run(SmDemoApplication.class, args);
}
}
And controller is
#RestController
public class WebRestController {
//#GetMapping("/hello")
#RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello() {
return "Hello World! 222";
}
}
http://localhost:8080 is good.
but, http://localhost:8080/hello makes white label error !!
what's the problem in my code?? (I made my project, using spring starter project)
Springboot reads its stereotype beans ,controllers and configurations from package and subpackages of main class,(until you define #ComponentScan to read other packages) I feel your project main class is in another package than the controller i.e. Controller is not in same or in a sub package where Main class is.
For example , If your main package is in
com.xyz.abc
|_ SmDemoApplication
Keep controller,other beans and configuration in com.xyz.abc package like
com.xyz.abc
|_WebRestController
or sub-packages like
com.xyz.abc.controllers
|_WebRestController
I tried to create simple Hello World app in Spring Boot using kotlin but IntelliJ IDE shows me warning that my controller class is never used and the specified endpoint does not work as well. I can't figure out what to do with it.
I created the app with the Boot Initializr and the structure looks like this:
kotlin/
com.myapp.school/
Application.kt
controller/
HelloController.kt
resources/
static/
templates/
hello.html
And here is the code for Application.kt:
package com.myapp.school
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
Last, I have this simple controller with one method:
package com.myapp.school.controller
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
#Controller
class HelloController
#GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
Going to localhost:8080/hello shows whitelabel error page with 404 status. I read that Spring prints registered endpoints into the console at startup but I haven't found such message.
Can somebody tell me what is wrong?
Thank you
I think your problem is that you have a top level class with no body (HelloController) and a top level function (hello). You have to put curly braces to make sure hello is a member of HelloController.
You have this:
#Controller
class HelloController
#GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
It needs to be like this, so hello belongs in HelloController, not at the same level:
#Controller
class HelloController {
#GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
}
Also, change System.out.println to just println to be more Kotlin-like.
I have a spring boot application having one of the component connecting to db. I want to avoid it's loading during tests run.
I am using below template for testing. Is it also possible to exclude the loading of particular component from main springboot class(MyApp.class)?
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(MyApp.class)
public class MyTest {
// ...Hitting some rest endpoint to extract data
}
Thanks in advance!
Assuming you are using JUnit:
#Component
#ConditionalOnMissingClass({"org.junit.Test"})
public class SomeComponent {
}
Or, if you activate a Spring profile for tests named test:
#Component
#Profile("!test")
public class SomeComponent {
}
Note, that the later is a Spring rather than spring-boot solution