https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

huehnerlady

07/03/2020, 3:54 PM
HI, I am quite new to kotlin. I have currently an generated Java interface containing spring Requestmapping annotations and extend this in my kotlin project. This works fine and the annotations are picked up and processed. When I generate the same interface as a kotlin interface though, these annotations seem to be ignored. Do I have to configure something to make this work?
✔️ 1
m

Margarita Bobova

07/08/2020, 6:06 AM
@huehnerlady Hello! Could you please share the code example?
h

huehnerlady

07/09/2020, 8:50 AM
@Margarita Bobova thanks for asking. So when I have a spring @RestController class and in there a method test like so:
Copy code
@RequestMapping(
            value = ["/test/{testId}"],
            produces = ["application/json"],
            method = [RequestMethod.GET])
    fun test( @PathVariable("testId") testId: String): ResponseEntity<String> {
        return ResponseEntity.ok(testId)
    }
Everything works But when I use a generated interface:
Copy code
@Validated
@RequestMapping("\${api.base-path:/v2/test}")
interface TestApi {
    @RequestMapping(
            value = ["/test/{testId}"],
            produces = ["application/json"],
            method = [RequestMethod.GET])
    fun test( @PathVariable("testId") testId: kotlin.String
): ResponseEntity<String> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}
and then override that method in the controller
Copy code
override fun test( @PathVariable("testId") testId: String): ResponseEntity<String> {
        return ResponseEntity.ok(testId)
    }
I get a 404 return. So the RequestMapping annotations in the interface seems to be ignored
m

Margarita Bobova

07/09/2020, 11:10 AM
@huehnerlady Thank you. I tried the same with java and get 404 too, so it seems like a Spring bug or restriction. Try to use
@GetMapping
in interface instead of a
@RequestMapping
like described in the article: https://www.baeldung.com/spring-interface-driven-controllers. It works.
h

huehnerlady

07/09/2020, 11:20 AM
@Margarita Bobova could you maybe share a code snippet? For me a swap of RequestMapping to GetMa[pping does not resolve it 😞
With Java it does work though … 🤔
this is the generation of java:
Copy code
@RequestMapping(value = "/test/{testId}",
            produces = { "application/json" },
            method = RequestMethod.GET)
    default ResponseEntity<String> test(@PathVariable("testId") String testId) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "null";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }
when I use it this way:
Copy code
override fun test(testId: String?): ResponseEntity<String> {
        return ResponseEntity.ok(testId?: "")
    }
Then I get a 200
m

Margarita Bobova

07/09/2020, 11:36 AM
@huehnerlady Here is a java project, changing to
@GetMapping
helps
Really, for Kotlin changing to
@GetMapping
does not help. Will investigate
h

huehnerlady

07/09/2020, 11:45 AM
I see, ok thank you. As said with a java interface it also wprks in kotlin with the @RequestMapping, it is just if the interface is also groovy 🙂 I strive to have a 100% kotlin service, and currently this problem is one of the obstacles I have to overcome 😞
m

Margarita Bobova

07/09/2020, 2:44 PM
@huehnerlady the following code works for me:
Copy code
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller

import org.springframework.web.bind.annotation.*

@Controller
class Controller : TestApi {

    override fun test(@PathVariable("testId") testId: String): ResponseEntity<String> {
        return ResponseEntity.ok(testId)
    }

}
Interface:
Copy code
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod

@Validated
@RequestMapping("/api")
interface TestApi {
    @RequestMapping(
            value = ["/test/{testId}"],
            produces = ["application/json"],
            method = [RequestMethod.GET])
    fun test( @PathVariable("testId") testId: kotlin.String
    ): ResponseEntity<String> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}
Could you please try?
h

huehnerlady

07/09/2020, 2:53 PM
hmm odd, for me I still get the 404 😞 I am using spring boot 2.2.6 and Spring 5.2.5. which version are you using?
m

Margarita Bobova

07/09/2020, 2:54 PM
spring boot 2.3.1
h

huehnerlady

07/09/2020, 2:54 PM
ok, will upgrade and have a look if that is the problem
m

Margarita Bobova

07/09/2020, 2:54 PM
could you please share the project?
h

huehnerlady

07/09/2020, 2:55 PM
unfortunately this is a closed project, so not easy to share. but if the spring upgrade does not work I will provide a sample project
m

Margarita Bobova

07/09/2020, 2:56 PM
yes, sample project would be great! The problem reproduced for me before, but doesn’t reproduce now
👍🏻 1
h

huehnerlady

07/09/2020, 3:41 PM
I tried to make a simple sample project but there it works. So I need to dive deeper into the code I guess, but for me it is very valuable to know, that it should work this way, so many many thanks for your help 🙂
👍 1
@Margarita Bobova I got it to work, it was a red hering as the kotlin-spring generator behaves differently from what they generate than the java-spring generator, that is why I had the problems. this is solved now. The problem was, that the spring generator does not generate a requestMapping on Controller level, the kotlin-spring generator does. Therefore the endpoint was not available at the point I was expecting, but at a different path Many many thanks 👍🏻
m

Margarita Bobova

07/17/2020, 7:14 AM
Thank you :)
20 Views