嗨我需要使用带有dropwizard的小胡子,但无法正确设置。 请看看......
service.java
public class Service extends Application<ServiceConfiguration>{
public static void main(String[] args) throws Exception {
new Service().run(args);
}
@Override
public void initialize(Bootstrap<ServiceConfiguration> bootstrap) {
bootstrap.addBundle(new ConfiguredAssetsBundle("/assets/", "/","index.html"));
bootstrap.addBundle(new ViewBundle<ServiceConfiguration>());
}
@Override
public void run(ServiceConfiguration config, Environment environment)throws Exception {
environment.jersey().register(new ViewResource());
}
}
ViewResource.java
package resource;
import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import view_object.UserSample;
import view_object.UsersView;
@Path("/views")
@Produces("text/html;charset=UTF-8")
public class ViewResource {
public ViewResource() {
super();
}
@GET
@Path("/usersample")
public UsersView fetch(){
List<UserSample> users = new LinkedList<UserSample>();
users.add(
new UserSample("user1","user1","Admin", "prit")
);
users.add(
new UserSample("user2","user2","DBA", "deepika")
);
return new UsersView(users);
}
}
UserView.java
package view_object;
import java.util.List;
import io.dropwizard.views.View;
public class UsersView extends View {
private final List<UserSample> users;
public UsersView(List<UserSample> users) {
super("sample.mustache");
this.users = users;
}
public List<UserSample> getUsers() {
return users;
}
}
UserSample.java
package view_object;
public class UserSample {
private String username;
private String password;
private String displayName;
private String displayRole;
public UserSample() {
}
public UserSample(String username, String password, String displayName,
String displayRole) {
this.username = username;
this.password = password;
this.displayName = displayName;
this.displayRole = displayRole;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDisplayRole() {
return displayRole;
}
public void setDisplayRole(String displayRole) {
this.displayRole = displayRole;
}
}
sample.mustache(Path是“assets / partial / sample.mustache”)
<html>
<head>
<title>Users</title>
</head>
<body>
<h1>Users</h1>
{{#users}}
<div>
<p>Username: {{username}}</p>
<p>Displayname: {{displayName}}</p>
</div>
{{/users}}
</body>
</html>
在yaml我指定了
server:
type: simple
applicationContextPath: /
adminContextPath: /admin
rootPath: '/api/*'
connector:
type: http
port: 9000
requestLog:
timeZone: UTC
appenders:
- type: console
threshold: ALL
timeZone: UTC
target: stdout
- type: file
currentLogFilename: ./log/requests.log
threshold: ALL
archive: true
archivedLogFilenamePattern: ./log/requests-%d.log.gz
archivedFileCount: 15
timeZone: UTC
assets:
overrides:
/: src/main/resource/assets/
并配置了ServiceConfiguration。
现在我的问题是当我点击“http://localhost:9000/api/views/usersample”
它抛出“无法找到/view_object/sample.mustache的渲染器”
请帮我解决我在这里做错了什么.. 我正在使用dropwizard 0.8.1,我也在maven中添加了相关的依赖项