A short question: Which annotation of Spring is ha...
# spring
j
A short question: Which annotation of Spring is handling the conversion between the Kotlin data class object and JSON? Or is this done by some other external library? I made a minimal example with Spring Boot + Kotlin, following this post https://grokonez.com/spring-framework/spring-boot/kotlin-springboot-jquery-ajax-postget. I wanted to learn about AJAX together with Spring Boot. I just changed the jQuery code into vanilla Javascript by myself. I chose in Spring Initializr: • Template Engine -> Thymeleaf (I’m not sure if this was really needed) • Web -> Web (MVC)
Copy code
@RestController
class RestWebController {

    val custStore = mutableListOf<Customer>()

    @GetMapping("/getallcustomer")
    fun getAllCustomer(): Response {
        println("GET: getallcustomer $custStore")
        return Response("Done", custStore)
    }

    @PostMapping("/postcustomer")
    fun postCustomer(@RequestBody customer: Customer): Response {
        println("POST: postcustomer $customer")
        custStore.add(customer)
        return Response("Done", customer)
    }
}
Copy code
data class Response(
    val status: String = "",
    val data : Any
)
Copy code
data class Customer(
    val firstname: String = "",
    val lastname: String = ""
)
m
The default library is Jackson. Ensure you include the jackson-kotlin dependency in your build file. Newer versions of Spring (2.1.5+ I think) will put a warning when you start the server if it detects Kotlin code, but not the jackson-kotlin dependency.
j
Thanks for the reply. There was the dependency in the Maven POM file, which was automatically generated. As a noob, I find it pretty difficult what is going under the hood, since all these external libraries like Jackson and dependencies are generated automatically. (Although I guess that’s what is intended for using Spring Boot, without really having to know all these stuffs to start) I couldn’t really find the relevant info just by googling. It’s really helpful that I could ask the community here.
And does the
return "index"
part here make the application look for
index.html
in my templates? Is this triggered by the
@Controller
annotation?
Copy code
@Controller
class WebController {

    @GetMapping("/")
    fun homepage(): String {
        return "index"
    }
}
m
Yes, Spring Boot (and other frameworks like it) are great for getting started quickly, BUT they're so massive, and do so much 'magic', that when things go wrong, or you want to understand it, it becomes difficult. For your
index
question, that depends on what other Spring dependencies you have brought in, but it's been a long time since I've look at templating options. I think the basic one will just return
"index"
as the response since you've defined an endpoint at
/
. If you didn't have that, then it would look in the
static
folder for an
index.html
j
I see. Maybe this is related with the Thymeleaf dependency that I brought in? Where, or how can I search about these templating options, such as which component or other library scans how for the templates? Any tips?
n
If you go to start.spring.io you will see other templating options. However, if you are using Webflux your choice may be limited.