Javalite ActiveWeb:子包中的自定义控制器不工作

Javalite ActiveWeb:子包中的自定义控制器不工作,javalite,activeweb,Javalite,Activeweb,我在app.config包中创建了一个RouteConfig类,如下所示: public class RouteConfig extends AbstractRouteConfig { @Override public void init(AppContext appContext) { route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById"); } package app.co

我在
app.config
包中创建了一个
RouteConfig
类,如下所示:

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

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}
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;
    }

}
public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }
route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");
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);
        }
    }
我创建了
AppControllerConfig
类:

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);
    }
}
我还定义了
APIController
类,如下所示:

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

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}
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;
    }

}
public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }
route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");
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);
        }
    }
AuthorsController
位于
app.controllers.api.v2
软件包中:

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);
        }
    }
}
尝试访问url时
http://localhost:8080/api/v2/authors/9
,我得到一个错误:

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.
我做错了什么?我甚至没有输入
AuthorsController
。。。 它似乎忽略了我的路线定义,并试图找到以下定义:

"method":"GET","action":"9","error":"java.lang.NoSuchMethodException: app.controllers.api.v2.AuthorsController.9()
在同一控制器中声明
索引
操作时,如下所示:

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

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}
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;
    }

}
public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }
route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");
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);
        }
    }

并点击
http://localhost:8080/api/v2/authors
,它可以工作。

最后,我想出了如何让它工作的方法

我在
RouteConfig
类中更改了路由声明,如下所示:

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

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}
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;
    }

}
public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }
route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");
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);
        }
    }
我将
AuthorsController
中的
findById
方法更改如下:

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

  route("/{controller}/{aut_id}/").to(AuthorsController.class).action("findById");
}
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;
    }

}
public void index() {
        final String json = Author.findFirst("AUT_ID = 9").toJson(false);
        respond(json);
    }
route("/{controller}/find-by-id}/{id}").to(AuthorsController.class).action("findById");
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);
        }
    }

按照惯例,作为url的数字传入的参数名称被认为是
id
而不是别的,对吗?

这在路由系统中清除了一个bug: 该错误已修复,新快照已部署到Sonatype版本:2.3-snapshot。


您现在可以尝试看看这是否有效。BWT,感谢您的详细描述,它帮助您建立了正确的规范:

啊,很酷,非常感谢,-很高兴我的问题对框架改进有所帮助。我来看看新的快照版本。关于如何用
2.3-snapshot
替换
pom.xml
中以前的依赖项,有什么提示吗?将其修改为
2.3-SNAPSHOT
时,它不起作用。谢谢。请按照这里的说明操作。我尝试了2.3-snapshot,但再次失败。这是我在
RouteConfig
中的路由定义:
route(“/{controller}/invoiceNumber/{invoiceNumber}”).to(InterventionsController.class).action(“findbyinoicenumber”)。以下是日志:
INFO org.javalite.activeweb.RequestDispatcher-{“INFO”:“executing controller”,“controller”:“app.controllers.api.v2.interventions controller”,“action”:“invoiceNumber”,“method”:“GET”}
。如您所见,调用的操作是错误的,-它没有查找
findbyinovoicenumber
,而是查找
invoiceNumber
Processed…,path:/api/v2/interferences/invoiceNumber/316
。我将更详细的信息放在了您关闭的文档中。这不准确。
id
可以是任何内容、字符串、数字等。