Search code examples
javajakarta-eejerseymultipartform-datamultipart

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.


Solution

  • 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) {}