一般Rest接口如何传递数组参数?
需要传递数组参数的情况,如果是一般的form表单,可以直接定义参数类型为List<String>即可(不能定义为数组类型,否则只能得到一个null)。
示例代码如下:
@POST
@Path("/user" )
public Response createUser(@FormParam("username" ) String username,
@FormParam( "keyword") List<String> keywords) {
文件上传接口如何传递数组参数?
但是如果是上传文件的接口,因为使用了@FormDataParam注解,则不能使用上述用法:
@POST
@Path("/upload" )
@Consumes(MediaType.MULTIPART_FORM_DATA )
public Response uploadFile(@FormDataParam ("filename" ) String filename,
@FormDataParam( "file") InputStream uploadedInputStream,
@FormDataParam( "file") FormDataContentDisposition fileDetail ,
@FormDataParam( "keyword") final List<String> keywords) {
访问这个接口时会发生异常:
java.lang.IllegalArgumentException : wrong number of arguments
可见使用@FormDataParam注解时,将参数类型定义成List<String>是不行的,可以定义成List<FormDataBodyPart>,从FormDataBodyPart对象中可以取得参数:
@POST
@Path("/upload" )
@Consumes(MediaType.MULTIPART_FORM_DATA )
public Response uploadFile(@FormDataParam("filename" ) String filename,
@FormDataParam( "file") InputStream uploadedInputStream,
@FormDataParam( "file") FormDataContentDisposition fileDetail ,
@FormDataParam( "keyword") final List<FormDataBodyPart> keywordObjs ) {
if (keywordObjs != null && ! keywordObjs.isEmpty()) {
for (FormDataBodyPart keywordObj : keywordObjs) {
String keyword = keywordObj.getValueAs(String.class);
System. out.println( "关键字:" + keyword );
}
}
分享到:
相关推荐
主要介绍了Restful传递数组参数及注解大全的相关资料,需要的朋友可以参考下
开发restful接口应该遵循统一的规范,保持规范的统一才能方便维护和应用
python开发,基于flask-restful 的中小型项目,restful风格API接口开发实例,以mongodb作为数据库 python开发,基于flask-restful 的中小型项目,restful风格API接口开发实例,以mongodb作为数据库python开发,基于...
httpclient和RestfuL服务风格上传下载文件,可改为JAVA分布式系统进行交互,具体用法和改法详见代码里的注释
jersey构建简单的Restful服务!
一个RESTful的文件下载方法
restful接口示例代码restful接口示例代码restful接口示例代码restful接口示例代码
主要介绍了Python中Flask-RESTful编写API接口(小白入门),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
REST它是一种使用URL来定位资源,使用HTTP请求描述操作的Web服务规范,本资源包含RESTful简介、设计原则、通用说明、规范细则、接口管理说明。
使用SpringBoot整合jersey 实现Restful web service.同时整合springmvc。
java jersey spring 集成 开发 restful API ,并将restful 返回的数据以JSON的格式返回,开发非常简单,易懂。欢迎小伙伴们 下载资源。
RESTFUL接口文档模板,样式好看的接口文档模板,docx格式
这是一个restful风格的webservice框架,使用官方的jersey 整合mybatis和spring,需要用tomcat8才能运行,
postman基于restful传递多个参数至thingworx方法,通过postman软件测试thingworx相应的接口数据
此代码通过c c++实现一个简单的http服务,可以用此实现一个restful api 服务器.
NC65通过自带的rest文件检索restful接口,实现对外开放的restful接口,使用方式和其他web系统的使用方法一致,只是规范了访问链接格式
Spring Hibernate, Jersey 创建restful 服务的例子 图文并茂,step by step
restful api 接口说明. 总结 restful api 语法知识和常用的状态码含义.
jersey的使用简单实例
jersey RESTful service 源代码 jersey RESTful service 源代码