简体   繁体   中英

Is there a method to read form data from HttpServletRequest?

I'm receiving the Form Data by adding @FormDataParam to a parameter of the REST Interface.

Our companies code guidelines state, that we should not use more then 7 parameters in a method. So I would like to reduce the number of parameters for this REST Interface.

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response upload(@Context final HttpServletRequest request, @FormDataParam("file") final java.io.File file, @FormDataParam("file") final FormDataContentDisposition fileMetaData, @FormDataParam("file") final FormDataBodyPart formDataBodyPart, @FormDataParam("networksegments") final String networksegments, @FormDataParam("users") final String users, @FormDataParam("username") final String username, @FormDataParam("token") final String token) {
    //Some code
}

I´m searching for a method like request.getFormData("name") to make the parameter obsolete. The above code is working fine, I want optimization.

You can create you own bean with @FormDataParam parameters and add to bean and then use one @BeanParam for all parameters in endpoint

 class OrderBean { @FormDataParam("clientName") private String clientName; // getter/setters } @POST public Response post(@BeanParam OrderBean order) {} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM