1

I want to load my table from a JSON that returns my REST web services in SpringMVC, but I get the following error that says that my rest method doesn't support the GET method.

The URL that my controller have mapped is this one

http://localhost:8080/MyApp/doc/showDocTable/

and the URL changes from that to this

http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1

I don't know why the URL changes to that since I'm doing just the basic example from the website http://ng-table.com/#/loading/demo-external-array

Here is the function in my controller that calls the webservices

var Api = $resource('http://localhost:8080/MyApp/doc/showDocTable/');
            this.tableParams = new NgTableParams({}, {
                getData: function (params) {
                    // ajax request to api
                    return Api.get(params.url()).$promise.then(function (data) {
                        params.total(data.inlineCount); // recal. page nav controls
                        return data.results;
                    });
                }
            });

And here is my controller method in Spring MVC

@RequestMapping(value = "/doc/showDocTable/", method = RequestMethod.GET)
public ResponseEntity<List<HistDoc>> showTable() {
    List<HistDoc> listHistDoc = docDAO.getDocs();


    return new ResponseEntity<List<HistDoc>>(listaHistDoc, HttpStatus.OK);
}

And here is the error that I get in my browser

GET XHR  http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1 [HTTP/1.1 405 Request method &#39;GET&#39; not supported 6ms]

and here the error in my console

WARN    2016-08-11 12:46:58,206 [http-listener-1(4)] org.springframework.web.servlet.PageNotFound  - Request method 'GET' not supported

I think the problem is that somehow ngTable changes the URL to that weird URL and my web method in Spring doesn't know what to do.

EDIT 1 I removed the "/" from my backend method url like this `@RequestMapping(value = "/doc/showDocTable", method = RequestMethod.GET) public ResponseEntity> showTable() { List listHistDoc = docDAO.getDocs();

    return new ResponseEntity<List<HistDoc>>(listaHistDoc, HttpStatus.OK);
}`

and also I removed the "/" from the getData method in angularJs controller

var Api = $resource('http://localhost:8080/MyApp/doc/showDocTable');

But now I get this error in my browser firefox console

Error: $resource:badcfg
Response does not match configured parameter

Error in resource configuration for action `get`. Expected response to contain an object but got an array (Request: GET http://localhost:8080/MyApp/doc/showDocTable)

EDIT 2: Here is my HTML

<div class="col-lg-12" ng-controller="EstHistDoc as demo">

    <table ng-table="demo.tableParams" class="table table-bordered table-striped table-condensed">
        <tr ng-repeat="row in $data track by row.idDoc">
            <td data-title="'Name'" filter="{name: 'text'}" sortable="'name'">{{row.name}}</td>
            <td data-title="'Description'" filter="{description: 'text'}" sortable="'description'">{{row.description}}</td>
        </tr>
    </table>

and here is the JSON that I return from the backend

    [
   {
      "idHisReg":null,
      "idDoc":1,
      "name":doc one,
      "description:" "a description"
   },
   {
      "idHisReg":null,
      "idDoc":2,
      "name": doc two,
      "description:" "a seconddescription"
   }
]
Bill_Data23
  • 659
  • 2
  • 14
  • 30
  • It's not a problem with the URL, since it only adds query parameters. Can you test the same URL with an application like postman or Advanced Rest Client (Chrome plugin)? Do you see the json response in the Developer tools? – Skaparate Aug 11 '16 at 17:47
  • the URL is fine since I already did unit testing to that method and if I access to that URL (http://localhost:8080/MyApp/doc/showDocTable/) I get the JSON response just fine I tested it with firebug, also I already use that URL but with another table plugin (angular-datatables) and I was able to load the table with that URL , but I want to use ngTable now but I get that error – Bill_Data23 Aug 11 '16 at 17:52
  • Can you debug/log the requested url in the backend? I think I was wrong about the query params, as they might be the problem. But I believe that your backend does not support the query params and thus doesn't recognize the endpoint. Try to see if the right method gets called. If it doesn't, then try to add the params to the $resource(url, { page: '@page', count: '@count' }) – Skaparate Aug 11 '16 at 18:10
  • what URL do you refer? the one with the parameters, because when I try to access this url `http://localhost:8080/MyApp/doc/showDocTable/?count=10&page=1` the one with the parameters I get this error in my console in my backend `WARN 2016-08-11 12:46:58,206 [http-listener-1(4)] org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported` – Bill_Data23 Aug 11 '16 at 18:14
  • Sorry, yes I did refer to that one. What if you remove the trailing slash? Like this: `http://localhost:8080/MyApp/doc/showDocTable?count=10&page=1` Does the backend work with it? – Skaparate Aug 11 '16 at 18:26
  • I edited my question, I removed the "/" but now I get this error Error: $resource:badcfg Response does not match configured parameter Error in resource configuration for action `get`. Expected response to contain an object but got an array – Bill_Data23 Aug 11 '16 at 18:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120733/discussion-between-bill-data23-and-skaparate). – Bill_Data23 Aug 11 '16 at 18:57

2 Answers2

0

Use Api.query(...) instead of using Api.get() should solve your error:

Error in resource configuration for action get. Expected response to contain an object but got an array (Request: GET http://localhost:8080/MyApp/doc/showDocTable)

colxi
  • 7,640
  • 2
  • 45
  • 43
0

You must use $resource.query() (Api.query()) instead of $resource.get() (Api.get()).

Skaparate
  • 489
  • 3
  • 14