标签: dropwizard

Dropwizard file upload

I have to upload a file from my site yet cnt seem to get it working with drop wizard.

Here is the form from my site.

   <form enctype="multipart/form-data" method="POST" action="UploadFile">
     <input type="file" id="fileUpload" name="file"/>
     <input type="hidden" id="fileName" name="fileName"/>
     <input type="submit" value="Upload"/>
   </form>
Run Code Online (Sandbox Code Playgroud)

How would I go about on the backend to receive the file?

The solution was

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") final InputStream fileInputStream,
        @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {


    String filePath = uploadLocation + newFileName;
    saveFile(fileInputStream, filePath);
    String …
Run Code Online (Sandbox Code Playgroud)

dropwizard

20
推荐指数
3
解决办法
1万
查看次数

使用dropwizard覆盖带有env变量的服务器连接器配置

我已经在dw邮件列表上发布了这个问题,但没有得到答案.

我可以假设下面的YML格式不再适用于DW 0.7.0吗?(使用@ char插入env var)

server:
  applicationConnectors:
    - type: http
      bindHost: @OPENSHIFT_DIY_IP@
      port: @OPENSHIFT_DIY_PORT@
Run Code Online (Sandbox Code Playgroud)

错误:

格式错误的YAML在第28行,第17列; 扫描下一个标记时; 找到无法启动任何令牌的字符@'@'.(不要使用@代替缩进); 在'reader',第28行,第17列:bindHost:@ OPENSHIFT_DIY_IP @

所以我决定使用这种格式:

server:
  type: simple
  applicationContextPath: /
  adminContextPath: /admin
  connector:
      type: http
      bindHost: localhost
      port: 8080
Run Code Online (Sandbox Code Playgroud)

并尝试通过jvm选项覆盖它:

java -Ddw.server.connector.bindHost=$OPENSHIFT_DIY_IP -Ddw.server.connector.port=$OPENSHIFT_DIY_PORT -jar target/myapp.jar server myapp.yml
Run Code Online (Sandbox Code Playgroud)

我的本地env变量:

OPENSHIFT_DIY_IP=localhost
OPENSHIFT_DIY_PORT=8080
Run Code Online (Sandbox Code Playgroud)

我从这个设置得到的错误:

线程"main"中的异常java.lang.RuntimeException:java.net.SocketException:org.eclipse.jetty.setuid.SetUIDListener.lifeCycleStarting(SetUIDListener.java:213)中未解析的地址...引起:java.net.SocketException :sun.nio.ch.Net.translateToSocketException(Net.java:157)中未解析的地址... WARN [2014-05-03 20:11:19,412] org.eclipse.jetty.util.component.AbstractLifeCycle:FAILED org .eclipse.jetty.server.Server @ 91b85:java.lang.RuntimeException:java.net.SocketException:未解析的地址

我究竟做错了什么?

java dropwizard

18
推荐指数
2
解决办法
9558
查看次数

使dropwizard中的cors无法正常工作

我正在研究一个dropwizard应用程序和js ui来与api交互.我需要加载json数据来更新视图,但我必须在之前启用dropwizard中的cors.我做了一些工作人员,但似乎没有工作因为dropwizard总是返回204没有内容.

@Override
public void run(final BGConfiguration configuration, final Environment environment) throws Exception {
  final Map<String, String> params = new HashMap<>();
  params.put("Access-Control-Allow-Origin", "/*");
  params.put("Access-Control-Allow-Credentials", "true");
  params.put("Access-Control-Expose-Headers", "true");
  params.put("Access-Control-Allow-Headers", "Content-Type, X-Requested-With");
  params.put("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  environment.servlets().addFilter("cors", CrossOriginFilter.class).setInitParameters(params);
}
Run Code Online (Sandbox Code Playgroud)

java dropwizard

18
推荐指数
2
解决办法
1万
查看次数

当前没有会话绑定到执行上下文

我用的时候得到了以下异常session.getCurrentSession().

我已经提到了

hibernate.current_session_context_class: managed

org.hibernate.HibernateException: No session currently bound to execution context
    at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:75)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at io.dropwizard.hibernate.AbstractDAO.currentSession(AbstractDAO.java:36)
    at io.dropwizard.hibernate.AbstractDAO.persist(AbstractDAO.java:149)
Run Code Online (Sandbox Code Playgroud)

我用它与dropwizard.任何人都可以帮我解决这个问题吗?

java session spring hibernate dropwizard

18
推荐指数
2
解决办法
1万
查看次数

Dropwizard - 组织你的项目,理解术语等

我正在学习使用Dropwizard.我能够遵循快速入门指南并运行基本的REST API.

在本文档中,有一个名为"组织项目"的部分.

它建议按以下部分组织项目:project-api,project-client,project-service.

这是我的问题/疑问:

  1. 一般来说,请解释'api','service'和'client'之间的区别.

  2. 是否有一个例子严格遵循上述惯例使用dropwizard?

  3. "...项目客户端应该使用这些类和HTTP客户端为您的服务实现一个成熟的客户端" - 因为"项目服务"将具有REST API,那么为什么我们需要使用HTTP客户端?

谢谢!

java rest http dropwizard

16
推荐指数
2
解决办法
9805
查看次数

如何在Dropwizard中进行资源的基本身份验证

我相信我有基本的身份验证工作,但我不知道如何保护资源,以便只有在用户登录时才能访问它们.

public class SimpleAuthenticator implements Authenticator<BasicCredentials, User> {
    UserDAO userDao;

    public SimpleAuthenticator(UserDAO userDao) {this.userDao = userDao;}

    @Override
    public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException    
    {
        User user = this.userDao.getUserByName(credentials.getUsername());
        if (user!=null &&
                user.getName().equalsIgnoreCase(credentials.getUsername()) &&
                BCrypt.checkpw(credentials.getPassword(), user.getPwhash())) {
            return Optional.of(new User(credentials.getUsername()));
        }
        return Optional.absent();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Signin资源是这样的:

@Path("/myapp")
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {
    @GET
    @Path("/signin")
    public User signin(@Auth User user) {
        return user;
    }
}
Run Code Online (Sandbox Code Playgroud)

我签署了用户:

~/java/myservice $ curl -u "someuser" http://localhost:8080/myapp/signin
Enter host password for user 'someuser':
{"name":"someuser"}
Run Code Online (Sandbox Code Playgroud)

假设用户使用/myapp/signin …

java authentication rest dropwizard

16
推荐指数
1
解决办法
1万
查看次数

DropWizard可以从jar文件外部提供资源吗?

在查看文档时,DropWizard似乎只能提供src/main/resources中的静态内容.我想将我的静态文件保存在jar文件之外的单独目录中.那可能吗?或者大多数人使用nginx/Apache作为他们的静态内容?

static-content dropwizard

15
推荐指数
2
解决办法
1万
查看次数

Dropwizard中应用程序和服务的区别

我是Dropwizard的新手.在最新的文档中,它将"服务"称为任何应用程序的主要入口点.但在示例代码中,它实际上使用"应用程序".我假设"应用程序"是"服务"的新名称,因为我在新的源代码中找不到"服务".

我还注意到命名空间已从"com.yammer"更改为"com.codehaus"更改为"io.dropwizard".我假设它反映了项目本身的演变.出于好奇,任何人都可以添加一些上下文来说明这一点吗?

java dropwizard

15
推荐指数
1
解决办法
5323
查看次数

使用带有JDBI/IDBI/Dropwizard的事务 - 回滚问题

我在使用IDBI处理事务时遇到了很多麻烦.我们正在使用dropwizard框架,简单的插入,更新,选择和删除已找到工作,但现在我们似乎无法使事务正常工作.这是我正在尝试的

public class JDb {
    private JustinTest2 jTest2 = null;
    private Handle dbHandle = null;

    public JDb(final IDBI idbi) {
        try {
            dbHandle = idbi.open();
            dbHandle.getConnection().setAutoCommit(false);
            jTest2 = dbHandle.attach(JustinTest2.class);
        } catch( SQLException e ) {

        }
    }

   public void writeJustin(final int styleId, final int eventId) {
        dbHandle.begin();
        int num = jTest2.findByStyleId(styleId);

        try {
            jTest2.doStuff(styleId, eventId);
            dbHandle.commit();
        } catch(Exception e) {
            dbHandle.rollback(); // Never rolls back here, always get the inserted row!
        }

        num = jTest2.findByStyleId(styleId); 
   } 
}
Run Code Online (Sandbox Code Playgroud)

这是我的JustinTest2课程

public abstract …
Run Code Online (Sandbox Code Playgroud)

java mysql transactions jdbi dropwizard

15
推荐指数
2
解决办法
1万
查看次数

使用Response参数的方法中的IllegalStateException

我写了一个简单的类来测试响应读取实体方法(如果它按预期工作).但它没有奏效.

当我启动课程时,我收到以下错误response.readEntity():

Exception in thread "main" java.lang.IllegalStateException: Method not supported on an outbound message.  
  at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:150)
Run Code Online (Sandbox Code Playgroud)

这是我写的代码

public static void main(String[] args) {
        List<Entity> representations = new ArrayList<>();
        representations.add(new Entity("foo", "baz", false));
        representations.add(new Entity("foo1", "baz1", true));
        representations.add(new Entity("foo2", "baz2", false));
        Response build = Response.ok(representations).build();
        printEntitesFromResponse(build);
    }

public static void printEntitesFromResponse(Response response) {
        response
                .readEntity(new GenericType<List<Entity>>() {})
                .stream()
                .forEach(entity -> System.out.println(entity));
    }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

java jax-rs dropwizard

15
推荐指数
1
解决办法
1万
查看次数