Spring boot Kotlin Spring引导表单urlencoded POST请求与映射
我刚刚开始使用Kotlin和Spring Boot,并决定编写一个简单的端点,该端点采用URL编码的POST请求形式。我不想为主体编写一个实际的数据类,所以我尝试只使用主体的映射,希望可以访问键/值对。我首先尝试:Spring boot Kotlin Spring引导表单urlencoded POST请求与映射,spring-boot,kotlin,Spring Boot,Kotlin,我刚刚开始使用Kotlin和Spring Boot,并决定编写一个简单的端点,该端点采用URL编码的POST请求形式。我不想为主体编写一个实际的数据类,所以我尝试只使用主体的映射,希望可以访问键/值对。我首先尝试: @RestController class MyController { @RequestMapping(value = "/endpoint", method = arrayOf(RequestMethod.POST), consumes = ar
@RestController
class MyController {
@RequestMapping(value = "/endpoint", method = arrayOf(RequestMethod.POST),
consumes = arrayOf("application/x-www-form-urlencoded"))
fun myEndpoint(@RequestBody body: Map<String,String>): String {
// Do stuff
}
}
@RestController
类MyController{
@RequestMapping(value=“/endpoint”,method=arrayOf(RequestMethod.POST),
consumes=arrayOf(“application/x-www-form-urlencoded”))
fun myEndpoint(@RequestBody:Map):字符串{
//做事
}
}
但这导致了一个关于不支持的媒体类型的415错误…我读到这是由于使用了@RequestBody和formurlencoded POSTs。我随后尝试使用@modeldattribute,但随后收到
未能实例化[java.util.Map]:指定的类是接口
这并不奇怪,因为我完全是黑客。我也尝试了不为主体添加任何注释,但是没有注入任何形式参数。我知道我可以添加一个数据类来解决这个问题,但我想知道是否可以像我以前在Java中做过类似的事情一样,使用映射来完成这个问题
谢谢。您需要用
@RequestParam
@RequestMapping(value = "/endpoint", method = [(RequestMethod.POST)],
consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
fun myEndpoint(@RequestParam body: Map<String,String>): String {
return //...
}
@RequestMapping(value=“/endpoint”,method=[(RequestMethod.POST)],
使用=[MediaType.APPLICATION\u FORM\u URLENCODED\u VALUE])
fun myEndpoint(@RequestParam body:Map):字符串{
返回/。。。
}
(还请注意,我删除了arrayOf
调用,以支持数组文字,从Kotlin 1.2开始,这些文字可用于注释)