如何在Java中使用paramType=”body”的Swagger注解传递List

huangapple go评论87阅读模式
英文:

How to pass a List<String> in swagger annotations in java with paramType="body"

问题

如何将List作为GET请求的输入参数?我尝试了以下方式,但不起作用。

我还尝试了将数据类型设置为Array[string],在这种情况下整个输入变成了一个字符串。

@GetMapping("/list/test") 
@ApiImplicitParams(value = {
        @ApiImplicitParam(name = "idList", dataType = "java.util.List<String>", paramType = "body")
})
public String getList(HttpServletRequest request, @RequestBody List<String> idList) {

    // 使用idList
}

以下是堆栈跟踪:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String aero.sita.referential.webservices.dr.controller.GraphApiController.groupMembers(javax.servlet.http.HttpServletRequest, java.util.List<java.lang.String>)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:153)
...
英文:

How to pass List&lt;String&gt; as an input for a get request? I tried the below way which is not working.

I have tried datatype as Array[string] as well, in that case whole input becomes a string.

@GetMapping(&quot;/list/test&quot;) 
@ApiImplicitParams(value = {
		@ApiImplicitParam(name = &quot;idList&quot;, dataType = &quot;java.util.List&lt;String&gt;&quot;, paramType = &quot;body&quot;)
		})
public String getList(HttpServletRequest request,@RequestBody List&lt;String&gt; idList) {

	// use idList
}

Below is the stack trace:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String aero.sita.referential.webservices.dr.controller.GraphApiController.groupMembers(javax.servlet.http.HttpServletRequest,java.util.List&lt;java.lang.String&gt;)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:153)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:127)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

答案1

得分: 3

使用@RequestParam而不是

@RequestParam(value = "myList") List<String> myList

Query Param中传递列表

url = "/list/test?myList=textA,textB"

Request Body中传递列表

$.ajax({
  type: "POST",
  url: "/list/test",
  ...
  data: {myList[0]: "textA",
         myList[1]: "textB"}
  ...
});
英文:

use @RequestParam instead

@RequestParam(value = &quot;myList&quot;) List&lt;String&gt; myList

pass a list in Query Param

url = &quot;/list/test?myList=textA,textB&quot;

pass a list in Request Body

$.ajax({
  type: &quot;POST&quot;,
  url: &quot;/list/test&quot;,
  ...
  data: {myList[0]: &quot;textA&quot;,
         myList[1]: &quot;textB&quot;}
  ...
});

huangapple
  • 本文由 发表于 2020年8月4日 00:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定