spring-hateoas icon indicating copy to clipboard operation
spring-hateoas copied to clipboard

Cannot build link using Spring-MVC and router functions dsl

Open NoxFr opened this issue 1 year ago • 1 comments

Trying to add spring-hateoas in a Spring-Mvc / Kotlin / router functions DSL context.

Constructing link with :

fun Survey.toDto(): SurveyDto {
    return SurveyDto(
        id = id,
        description = description,
        questions = questions.map { it.toDto() },
        tags = tags.map { it.toDto() },
        state = state.name,
        createdAt = createdAt,
        updatedAt = updatedAt
    ).add(SurveyHandler::class) {
        linkTo { getSurvey(id.toString()) } withRel SELF
    }
}

With SurveryService

@Component
class SurveyHandler(private val surveyService: SurveyService) {
    private val logger = KotlinLogging.logger {}

    fun getSurvey(id: String): SurveyDto {
        logger.info { "Calling GET survey with id: $id" }
        return surveyService.findSurvey(UUID.fromString(id)).toDto()
    }

And Routing

@Configuration
class RoutesConfiguration(private val surveyHandler: SurveyHandler) {
    @Bean
    fun surveyRoutes() = router {
        "/v1/admin/survey".nest {
            GET("/{id}") {
                ok().body(surveyHandler.getSurvey(it.pathVariable("id")))
            }
        }
        onError<Exception>(::handleExceptions)
    }

}

Throws exception java.lang.IllegalArgumentException: No mapping found for public com.liksi.questionr.server.survey.dto.SurveyDto com.liksi.questionr.server.survey.handler.SurveyHandler.getSurvey(java.lang.String)!

As seen here it seems to only handle @RequestMapping annotated method : https://github.com/spring-projects/spring-hateoas/blob/main/src/main/java/org/springframework/hateoas/server/core/SpringAffordanceBuilder.java

Am i missing something ?

NoxFr avatar Apr 26 '23 10:04 NoxFr

You don't. There's currently no support for that, as we rely on being able to find the mapping information by inspecting the methods of the class pointed to by the method used in linkTo. I'll have to take this back to the core Spring team for options how to fix that.

odrotbohm avatar May 03 '23 16:05 odrotbohm