1

我是 Dropwizard 的新手,我想做的是按照教程 ( http://dropwizard.codahale.com/manual/views/#manual-views ) 来实现简单的基于 freemarker 的视图。我完全按照它说的做了,但是当我启动我的应用程序并将浏览器导航到http://localhost:8080/user 码头时,我返回 404。

package com.example.views;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/user")
@Produces(MediaType.TEXT_HTML)
public class UserResource
{

    @GET
    public UserView getUser()
    {
        return new UserView();
    }
}


package com.example.views;

import com.yammer.dropwizard.views.View;

public class UserView extends View {

    public UserView() {
        super("user.ftl");
    }

}

package com.example.service;

import com.yammer.dropwizard.ConfiguredBundle;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.views.ViewBundle;

public class WebCoreApplication extends Service<WebCoreConfiguration> {
    private final ConfiguredBundle[] bundles;

    public WebCoreApplication(ConfiguredBundle...bundles) {
        this.bundles = bundles;
    }

    @Override
    public void initialize(Bootstrap<WebCoreConfiguration> bootstrap) {
        for(ConfiguredBundle bundle : bundles) {
            bootstrap.addBundle(bundle);
        }
        bootstrap.addBundle(new ViewBundle());
    }

    @Override
    public void run(WebCoreConfiguration configuration, Environment environment) throws Exception {
            }
}

我的 user.ftl 在 src/main/resources/com/example/views 文件夹中。

我做错了什么,我应该如何注册球衣资源以响应请求?

4

3 回答 3

2

示例 Dropwizard 服务

对于使用 Freemarker 视图的标准 v0.6.2 Dropwizard 应用程序,可以将以下代码视为样板:

public class OpenIDDemoService extends Service<OpenIDDemoConfiguration> {

  /**
   * Main entry point to the application
   *
   * @param args CLI arguments
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    new OpenIDDemoService().run(args);
  }

  private OpenIDDemoService() {

  }

  @Override
  public void initialize(Bootstrap<OpenIDDemoConfiguration> openIDDemoConfigurationBootstrap) {

    // Bundles
    openIDDemoConfigurationBootstrap.addBundle(new AssetsBundle("/assets/images", "/images"));
    openIDDemoConfigurationBootstrap.addBundle(new AssetsBundle("/assets/jquery", "/jquery"));
    openIDDemoConfigurationBootstrap.addBundle(new ViewBundle());
  }

  @Override
  public void run(OpenIDDemoConfiguration openIDDemoConfiguration, Environment environment) throws Exception {
    // Configure authenticator
    OpenIDAuthenticator authenticator = new OpenIDAuthenticator();

    // Configure environment
    environment.scanPackagesForResourcesAndProviders(PublicHomeResource.class);

    // Health checks
    environment.addHealthCheck(new uk.co.froot.demo.openid.health.OpenIdDemoHealthCheck());

    // Providers
    environment.addProvider(new ViewMessageBodyWriter());
    environment.addProvider(new OpenIDRestrictedToProvider<User>(authenticator, "OpenID"));

    // Session handler
    environment.setSessionHandler(new SessionHandler());  }
}

希望能帮助到你。

于 2013-07-19T05:28:00.843 回答
1

我发现您必须在网址中添加“应用程序”

http://localhost:8080/application/test
于 2014-09-20T17:01:31.150 回答
0

通过添加修复它

environment.addResource(new UserResource()); 

到我的run()方法。

于 2013-07-18T19:52:05.700 回答