huehnerlady
07/03/2020, 3:54 PMMargarita Bobova
07/08/2020, 6:06 AMhuehnerlady
07/09/2020, 8:50 AM@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:
@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
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 ignoredMargarita Bobova
07/09/2020, 11:10 AM@GetMapping
in interface instead of a @RequestMapping
like described in the article: https://www.baeldung.com/spring-interface-driven-controllers. It works.huehnerlady
07/09/2020, 11:20 AM@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:
override fun test(testId: String?): ResponseEntity<String> {
return ResponseEntity.ok(testId?: "")
}
Then I get a 200Margarita Bobova
07/09/2020, 11:36 AM@GetMapping
helps@GetMapping
does not help. Will investigatehuehnerlady
07/09/2020, 11:45 AMMargarita Bobova
07/09/2020, 2:44 PMimport 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:
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?huehnerlady
07/09/2020, 2:53 PMMargarita Bobova
07/09/2020, 2:54 PMhuehnerlady
07/09/2020, 2:54 PMMargarita Bobova
07/09/2020, 2:54 PMhuehnerlady
07/09/2020, 2:55 PMMargarita Bobova
07/09/2020, 2:56 PMhuehnerlady
07/09/2020, 3:41 PMMargarita Bobova
07/17/2020, 7:14 AM