标签: guice-servlet

使用Guice-servlet/Jetty/Jersey的轻量级Java Web堆栈 - 一些问题

我正在考虑使用"轻量级"组件而不是完整的堆栈框架来开发新的Web应用程序.

这篇文章是我的主要灵感!

Jetty:Web服务器.我可能会使用embedabble版本进行开发,但可以选择将应用程序导出为.war并使用外部Jetty服务器进行生产环境.

Guice/Guice-Servlet:用于依赖注入和servlet映射+过滤器.

Jersey:用于路由+请求/响应json(de)序列化时的需要.

重要提示:我知道有些人会用这种栈中的泽西作为Web服务层而已,而会使用JavaScript框架(骨干,AngularJS等)使用这些服务,并完成大部分的表示逻辑在Javascript中.我还没有为这种客户端做好准备.我仍然喜欢使用JSP,并且能够将纯HTML发送到禁用了javascript的客户端.

所以,我的问题:

  • 使用Jersey 管理表单的最佳方法是什么?使用Spring MVC(我在其他项目中使用),有一个"支持对象"的概念,其中提交的POST数据自动绑定到一个易于使用的支持对象.泽西有类似的东西吗?

  • 我喜欢在一个特定的路由文件中定义所有路由,而不是像@Path注释那样在我看来更难管理.我很确定Jersey要求使用那些硬编码的JAX-RS的@Path注释并且不允许外部路由配置系统,这是正确的吗?您是否认为我可以用泽西岛集中所有路线?

  • 我喜欢反向路由的概念(例如Play框架提供).而且,我认为泽西岛不能提供这种功能,这是正确的吗?

  • 考虑到我以前的问题,也许Jersey不适合使用?你知道我可以用于堆栈中的路由部分的其他库吗?

  • 有关这种轻量级Java Web堆栈的任何其他建议/提示吗?

更新:

我目前正在寻找UrlRewriteFilter作为路由部分.

我也在看ActiveWeb框架,它是一个"完整堆栈"框架,但看起来很轻,似乎也提供了一些我正在寻找的功能:集中式路由配置和反向路由.

java web-frameworks jersey guice guice-servlet

5
推荐指数
0
解决办法
2045
查看次数

Guice :是否有可能获得给定特定父/接口/注释的所有绑定实例?

我知道这可能不是最纯粹形式的依赖注入,但假设我必须使用:

@Inject
Injector injector;
Run Code Online (Sandbox Code Playgroud)

使用这个注入器,是否有可能获得所有给定的绑定实例:

  1. 每个实例都会继承的父类?
  2. 每个实例将实现的接口?
  3. 每个实例都有一个注释?

我看到了Injector#findBindingsByType()方法,但我不确定它在这方面是否有帮助。

guice guice-servlet

5
推荐指数
1
解决办法
6441
查看次数

Guice:如何绑定由已经绑定的对象动态获取的类?

我正在使用Guice开发一个小型Web框架.我有一个Router对象,一旦初始化,就会公开一个getControllerClasses()方法.我必须使用Guice将所有动态返回的类循环到bind()它们.

我绑定路由器:

bind(IRouter.class).to(Router.class);
Run Code Online (Sandbox Code Playgroud)

但是,如何在Module中获取绑定的Router实例,这样我还可以绑定其getControllerClasses()方法返回的类?

我能够在模块中获取路由器实例的唯一方法是在第一个模块中绑定此实例,然后在setter上使用@Inject将其注入第二个模块:

第1单元

bind(IRouter.class).to(Router.class);

Module2 module2 = Module2();
requestInjection(module2);
install(module2); 
Run Code Online (Sandbox Code Playgroud)

第2单元

@Inject
public void setRouter(IRouter router)
{
    Set<Class<?>> controllerClasses = router.getControllerClasses();
    for(Class<?> controllerClass : controllerClasses)
    {
        bind(controllerClass);
    }
}
Run Code Online (Sandbox Code Playgroud)

调用该方法并初始化Router实例,但控制器类的绑定失败!在Guice生命周期的这一步,似乎module2 的binder实例是NULL.

如何绑定动态获取的类,这些类是否已由已绑定的对象返回?

guice guice-servlet

5
推荐指数
1
解决办法
3462
查看次数

Guice Provider <EntityManager> vs EntityManager

我试图使用持久性和servlet guice扩展使用简单的webapp在Jetty上使用Guice和JPA.

我写了这个Service实现类:

public class PersonServiceImpl implements PersonService {

private EntityManager em;

@Inject
public PersonServiceImpl(EntityManager em) {
    this.em = em;
}

@Override
@Transactional
public void savePerson(Person p) {
    em.persist(p);
}

@Override
public Person findPerson(long id) {
    return em.find(Person.class, id);
}

@Override
@Transactional
public void deletePerson(Person p) {
    em.remove(p);
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的servlet(用@Singleton注释):

@Inject
PersonService personService;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String name = req.getParameter("name");
    String password = req.getParameter("password");
    String email = req.getParameter("email");
    int age = …
Run Code Online (Sandbox Code Playgroud)

java jpa guice guice-servlet guice-persist

5
推荐指数
1
解决办法
2581
查看次数

将Guice与Jersey(squarespace)集成:未安装ServiceLocatorGenerator

我正在部署一个Jersey 2.22应用程序,我试图使用Squarespace jersey2-guice-impl库顺利地与Guice 4.0集成,以便我可以轻松地将Guice依赖项注入我的控制器.

(我为下面的小错字道歉,我不得不重新输入所有内容.)

但是我在启动时遇到以下错误,这是由我的WebListener引起的:

   SEVERE: Exception starting filter jersey
   java.lang.IllegalStateException: It appears there is no ServiceLocatorGenerator installed.  
     at com.squarespace.jersey2.guice.GuiceServiceLocatorGeneratorStub.create(GuiceServiceLocatortGeneratorStub.java:50)
     at org.glassifsh.hk3.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:312)
     at org.glassfish.h2k.internal.ServiceLocatorFactoryimpl.create(ServiceLocatorFactoryImpl.java: 268)
     at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:138)
     at org.glassfish.internal.inject.Injections.createLocator(Injections.java:123)
     at org.glassfish.jersey.server.ApplicationHandler.<init>(WebComponent.java:392)
     at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
     at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer:415)
     at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
     at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java: 262)
     at ....
Run Code Online (Sandbox Code Playgroud)

我从我的pom.xml使用以下依赖项:

 <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guice</artifactId>
    <version>4.0.0</version>
 </dependency>
 <dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-servlet</artifactId>
    <version>4.0.0</version>
 </dependency>
 <dependency>
    <groupId>com.squarespace.jersey2-guice</groupId>
    <artifactId>jersey2-guice-impl</artifactId>
    <version>1.0.6</version>
 </dependency>
 <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.2</version>
 </dependency>
 <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.2</version>
 </dependency>
 <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.22.2</version>
 </dependency>
 <dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-mvc</artifactId>
    <version>2.22.1</version>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

这是WebListener: …

tomcat jersey guice squarespace guice-servlet

5
推荐指数
0
解决办法
601
查看次数

如何为Guice Servlet模块中定义的过滤器指定调度程序类型?

我正在开发基于Java的Web应用程序.我们正在使用GuiceServletModule来配置servlet和过滤器.

现在,Filter即使通过调度程序转发请求,我也需要调用,而不仅仅是传入的请求.

在简单的JEE Web应用程序中,我可能会这样设置web.xml...

<filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>com.acme.SomeFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

但是我不能在Guice中做同样的事情,我只能写那样的东西......

filter("/*").through(com.acme.SomeFilter.class);
Run Code Online (Sandbox Code Playgroud)

......在我看来,我无法明确指定FORWARD模式.因此,转发请求时过滤器不会启动.

你知道我是否有办法在Guice中实现这一目标?

提前致谢.

java guice servlet-filters guice-servlet

5
推荐指数
0
解决办法
204
查看次数

转发对JSP的请求

上周我发现了Guice ...我正在尝试一些简单的技巧.但是,我目前被封锁......

我正在尝试将请求转发到由包含" * " 的url-pattern服务的Servlet中的JSP .但我一直收到"错误404":(

一步步 :


ServletModule :
serve("/test/*").with(TestServlet.class);
Run Code Online (Sandbox Code Playgroud)
TestServlet :
public void doGet(HttpServletRequest req, HttpServletResponse resp)  
{

    System.err.println("Start");
    try 
    {
        req.getRequestDispatcher("/WEB-INF/layout/test.jsp").forward(req, resp);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

HTTP错误404
访问/WEB-INF/layout/test.jsp时出现问题.原因:/
WEB-INF/layout/test.jsp

我测试了"serve("/ test").with(TestServlet.class);" 它工作
我没有Guice测试(通过在web.xml中定义servlet),它工作...

  • 我做错了什么?

感谢阅读!

java jsp servlets guice guice-servlet

4
推荐指数
2
解决办法
3633
查看次数

如何在Wicket中使用Guice Servlet

在使用Guice Servlet设置我的Wicket项目之后,我得到了一个java.lang.IllegalStateException: filter path was not configured.该过滤器路径配置,虽然.我错过了什么吗?

web.xml中

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

    <display-name>node-sitter</display-name>

    <listener>
        <listener-class>com.mycompany.wicketapp.inject.ServletConfig</listener-class>
    </listener>
    <filter>
        <description>Initialises Guice</description>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

Guice Servlet听众:

public class ServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new Servlets());
    }

    private static class Servlets extends ServletModule {

        @Override
        protected void configureServlets() {
            bind(WicketFilter.class).in(Singleton.class);
            filterRegex("/.*").through(WicketFilter.class, withApplicationClass(WicketApplication.class));
        }

        private Map<String, String> withApplicationClass(Class<? extends WebApplication> applicationClass) {
            Map<String, String> initParams …
Run Code Online (Sandbox Code Playgroud)

java wicket jetty guice guice-servlet

4
推荐指数
1
解决办法
2734
查看次数