1

I created a RouteConfig class in app.config package as follows:

public class RouteConfig extends AbstractRouteConfig {
  @Override
  public void init(AppContext appContext) {

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}

I created AppControllerConfig class:

package app.config;

import app.controllers.CatchAllFilter;
import app.controllers.api.v2.AuthorsController;
import org.javalite.activeweb.AbstractControllerConfig;
import org.javalite.activeweb.AppContext;
import org.javalite.activeweb.controller_filters.DBConnectionFilter;

public class AppControllerConfig extends AbstractControllerConfig {

    public void init(AppContext context) {
        add(new DBConnectionFilter(), new CatchAllFilter()).to(AuthorsController.class);
    }
}

I also defined APIController class as follows:

package app.controllers;

import org.javalite.activeweb.AppController;

public abstract class APIController extends AppController {
    @Override
    protected String getContentType() {
        return "application/json";
    }

    @Override
    protected String getLayout() {
        return null;
    }

}

AuthorsController is in app.controllers.api.v2 package:

import app.controllers.APIController;

import app.models.Author;

public class AuthorsController extends APIController {

    public void findById() {
        if (blank("aut_id")) {
            throw new RuntimeException("aut_id number is mandatory but was empty or null");
        } else {
            Author author = Author.findById(param("aut_id"));
            String jsonResponse = author.toJson(false);
            respond(jsonResponse);
        }
    }
}

When trying to access the url http://localhost:8080/api/v2/authors/9, I'm getting the error:

Request properties: Request URL: http://localhost:8080/api/v2/authors/9
ContextPath: 
Query String: null
URI Full Path: /api/v2/authors/9
URI Path: /api/v2/authors/9
Method: GET

Request parameters: {}
Request headers: {Cookie=_ga=GA1.1.1833610632.1527846931; JSESSIONID=node03k88mhw4nihozrzi9ehiiadj0.node0, Cache-Control=no-cache, Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, Upgrade-Insecure-Requests=1, Connection=keep-alive, User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36, Host=localhost:8080, Pragma=no-cache, Accept-Encoding=gzip, deflate, br, Accept-Language=en,fr;q=0.9,ru;q=0.8}
org.javalite.activeweb.ActionNotFoundException: java.lang.NoSuchMethodException: app.controllers.api.v2.AuthorsController.9(); app.controllers.api.v2.AuthorsController.9()
[qtp1987707214-16] WARN ParamCopy - found 'session' value set by controller. It is reserved by ActiveWeb and will be overwritten.
[qtp1987707214-16] INFO FreeMarkerTemplateManager - rendering template: '/system/404' with layout: '/layouts/default_layout', session: node018slc2f7n5p0v67173ezr8guc0
[qtp1987707214-16] WARN FreeMarkerTemplateManager - Current location: /Users/Serguei/projects/java/newton-interventions
[qtp1987707214-16] ERROR RequestDispatcher - ActiveWeb internal error: 
org.javalite.activeweb.ViewMissingException: Failed to render template: '/system/404.ftl', with layout: '/layouts/default_layout'; Template "/system/404.ftl" not found.

What am I doing wrong ? I don't even enter the AuthorsController... It seems like it ignores my route definition and tries to find the following one:

"method":"GET","action":"9","error":"java.lang.NoSuchMethodException: app.controllers.api.v2.AuthorsController.9()

When declaring index action in the same controller as follows:

public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }

and hitting http://localhost:8080/api/v2/authors, it works.

belgoros
  • 3,590
  • 7
  • 38
  • 76

2 Answers2

1

This flushed a bug in the routing system: https://github.com/javalite/activeweb/issues/397 The bug was fixed and a new snapshot deployed to Sonatype version: 2.3-SNAPSHOT. https://oss.sonatype.org/content/repositories/snapshots/org/javalite/activeweb/2.3-SNAPSHOT/

You can now try to see if this works. BWT, thanks for a detailed description, it helped to setup the right spec: RouterCustomSpec

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Ah, cool, thanks a lot, - glad my issue to be helpful in framework improvement. I'll take a look at the new snapshot release. – belgoros Sep 18 '18 at 07:29
  • Any tips on how to replace the previous dependency in `pom.xml` by the `2.3-SNAPSHOT`? When modifying it as `2.3-SNAPSHOT`, it didn't work. Thank you. – belgoros Sep 18 '18 at 13:38
  • Please, follow instructions here http://javalite.io/releases#current-snapshot-2.3-snapshot – ipolevoy Sep 19 '18 at 05:05
  • I tried 2.3-snapshot and it fails again. Here is my route definition in `RouteConfig`: `route("/{controller}/invoiceNumber/{invoiceNumber}").to(InterventionsController.class).action("findByInvoiceNumber");`. Here is the logs: `INFO org.javalite.activeweb.RequestDispatcher - {"info":"executing controller","controller":"app.controllers.api.v2.InterventionsController","action":"invoiceNumber","method":"GET"}`. As you see, the called action is wrong, - instead of `findByInvoiceNumber`, it looked for `invoiceNumber`: `Processed ..., path: /api/v2/interventions/invoiceNumber/316`. – belgoros Sep 19 '18 at 12:03
  • I put more detailed information in the [issue](https://github.com/javalite/activeweb/issues/397) you closed. – belgoros Sep 19 '18 at 12:37
0

Finally, I figured out how to make it work.

I changed the route declaration in RouteConfig class as follows:

route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");

I changedfindById mehtod in AuthorsController as follows:

public void findById() {
        System.out.println(params());
        if (blank("id")) {
            throw new RuntimeException("autId number is mandatory but was empty or null");
        } else {
            Author author = Author.findById(param("id"));
            String jsonResponse = author.toJson(false);
            respond(jsonResponse);
        }
    }

It seems like by convention, the name of parameter passed in as a number to the url is considered as to be id and nothing else, right ?

belgoros
  • 3,590
  • 7
  • 38
  • 76