Search code examples
angularjsjax-rsng-grid

AngularJS and JAX-RS with ng-Grid


I am working on a AngularJS & JAX-RS application.(frankly am new to both)

On UI i have a ng-grid , each row has 3 columns , 1st a check box , 2nd Name , 3rd Age.

User can select rows from grid and click on "export to excel". I am posting the whole data when user clicks on export to excel button

@POST
@PATH("/xlsExport")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response excelExport(MultiPartFormData mfd) throws Exception {
        System.out.ptrintln(mfd); // code not reaching here
}

Am getting exception "RESTEASY003065 : Cannot Consume Contet Type" UI looks like

Also Tried

   @POST
    @PATH("/xlsExport")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response excelExport(String mfd) throws Exception {
            System.out.ptrintln(mfd); // code not reaching here
    }

Same error

Also Tried

   @POST
    @PATH("/xlsExport")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response excelExport(String mfd) throws Exception {
            System.out.ptrintln(mfd); // incorrect values shown
    }

This didn't give error, but also didn't give me all rows, just gives some incorrect values in .... so if i select 3 rows of 10 , i get some incorrect values in String as of now

Also Tried

   @POST
    @PATH("/xlsExport")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
    public Response excelExport(@FormParam("id") Sting id) throws Exception {
            System.out.ptrintln(id); // Compile time error
    }

Same error

<form novalidate name="someForm" method="POST" action="/xlsExport">
    ...
    <div class="ngHeaderContainer">
        <div> <input class="ngSelectoionHeader" .../> </div> <!-- checkbox -->
        <div> Name </div> 
        <div> Age </div> 
    </div>  
    <input type="submit" value="Export">
</form>

Will appreciate if can get some help

  1. How can i resolve the error
  2. Is this the best way to do it , cant i consume data in JSON

Solution

  • In order to send JSON (you're currently sending application/x-www-form-urlencoded but who knows what the plain HTML data looks like), you will need to handle the form submit event in your front-end app.

    To do this, remove the action attribute and use ng-submit to call a controller function which uses $http or similar to post the data...

    <form novalidate name="someForm" ng-submit="submit()">
    

    and something like this in your controller

    $scope.submit = function() {
        $http.post('/xlsExport', $scope.someDataModel).then(res => {
            // handle the response here
        });
    };