RESTful transfer array parameter and annotation encyclopedia _java

Source: Internet
Author: User
Tags file upload http post http request

RESTful a software architecture style, design style rather than standard, just provides a set of design principles and constraints. It is mainly used for client and server interaction class software. Software designed based on this style can be simpler, more layered, and easier to implement such mechanisms as caching.

If you need to pass an array parameter, if it is a generic form form, you can directly define the parameter type as list<string> (you cannot define an array type, or you can get only one null).

The sample code is as follows:

@POST
@Path ("/user") public
Response CreateUser (@FormParam ("username") String username,
      @FormParam (" Keyword ") list<string> keywords) {

How does the file upload interface pass data parameters?

However, if it is an interface for uploading files, the above usage cannot be used because @FormDataParam annotations are used:

@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) {

An exception occurs when accessing this interface:

Java.lang.IllegalArgumentException:wrong Number of arguments

When @FormDataParam annotations are used, it is not possible to define the parameter types as list<string>, which can be defined as list<formdatabodypart>, from Parameters can be obtained in the Formdatabodypart object:

@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
 (formdatab Odypart keywordobj:keywordobjs) {
   String keyword = keywordobj.getvalueas (string.class);
   System. Out.println ("keyword:" + keyword);
     }
   

RESTful Annotation Encyclopedia

@GET, @POST, @PUT, @DELETE, @HEAD

@GET, @POST, @PUT, @DELETE, and @HEAD are all HTTP request method indicator annotations. You can use them to bind Java methods and HTTP request methods within a root resource or child resource. HTTP GET requests are mapped to methods that are annotated by the @GET, and HTTP POST requests are mapped to methods that are @POST annotated, and so on.

@Conumes and @Produces

@Conumes annotation represents a MIME type that a resource can accept. @Produces annotation represents the MIME type that a resource can return.

@Path

@Path annotations are used to describe the root resource, child resource method, or child resource location. Value values can contain text characters, variables, or variables that have custom regular expressions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.