How to access POST parameters in HttpServletRequest?

前端 未结 1 709
鱼传尺愫
鱼传尺愫 2021-01-21 04:12

I\'ve got an app that\'s basically a proxy to a service. The app itself is built on Jersey and served by Jetty. I have this Resource method:

@POST
@Path(\"/{defa         


        
相关标签:
1条回答
  • 2021-01-21 04:42

    Three options

    1. @FormParam("<param-name>") to gt individual params. Ex.

      @POST
      @Consumes("application/x-www-form-urlencoded")
      public Response post(@FormParam("foo") String foo
                           @FormParam("bar") String bar) {}
      
    2. Use a MultivaluedMap to get all the params

      @POST
      @Consumes("application/x-www-form-urlencoded")
      public Response post(MultivaluedMap<String, String> formParams) {
          String foo = formParams.getFirst("foo");
      }
      
    3. Use Form to get all the params.

      @POST
      @Consumes("application/x-www-form-urlencoded")
      public Response post(Form form) {
          MultivaluedMap<String, String> formParams = form.asMap();
          String foo = formParams.getFirst("foo");
      }
      
    4. Use a @BeanParam along with individual @FormParams to get all the individual params inside a bean.

      public class FormBean {
          @FormParam("foo")
          private String foo;
          @FormParam("bar")
          private String bar;
          // getters and setters
      }
      
      @POST
      @Consumes("application/x-www-form-urlencoded")
      public Response post(@BeanParam FormBean form) {
      }
      
    0 讨论(0)
提交回复
热议问题