有没有办法从客户端控制输出格式?
我有一节课
@Produces(MediaType.TEXT_HTML)
Run Code Online (Sandbox Code Playgroud)
我希望它在客户端请求时生成json.
我可以逐字复制该类,只替换@Path和@Produces注释,但这看起来完全是浪费.
我想知道客户端是否可以将类似&content-type = application/json的内容添加到URL并让我的服务器使用json而不是html进行响应?
我有一个过滤器 - 为我的Web服务添加了TimeValidationFilter,如下所示:
@Override
public final void run(T configuration, Environment environment) throws Exception {
environment.addFilter(new TimeValidationFilter(), "/*");
environment.manage(***blablabla***);
environment.addHealthCheck(***blablabla***);
try {
environment.addResource(new BlablaResourceImpl(configuration));
} catch (ConfigurationException e) {
throw new IllegalStateException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我的TimeValidationFilter只是在这个阶段检查startTime和endTime是否为null:
public class TimeValidationFilter implements Filter {
public final static String START_TIME = "startTime";
public final static String END_TIME = "endTime";
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String startTimeStr = request.getParameter(START_TIME);
String endTimeStr = request.getParameter(END_TIME);
if …Run Code Online (Sandbox Code Playgroud) 我已经创建了 Dropwizard Resource 并将其映射到 REST API。现在我想从我的代码中的其他点重用这个资源 API 作为 JAVA API。我该怎么做?
这是资源类:
@Path("/providers_new")
public class ProviderResource {
private ProviderDAO dao;
public ProviderResource(ProviderDAO dao) {
this.dao = dao;
}
@GET
@Path("/get")
@Produces("application/json")
public List<Provider> getAll() {
return dao.getAllProviders();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意 ProviderResource 是用 dao 初始化的:
public class EntitiesService extends Service<EntitiesServiceConfiguration> {
public static void main(String[] args) throws Exception {
new EntitiesService().run(args);
}
@Override
public void initialize(Bootstrap<EntitiesServiceConfiguration> bootstrap) {
bootstrap.setName("entities");
...
}
@Override
public void run(EntitiesServiceConfiguration configuration,
Environment environment) throws ClassNotFoundException {
final …Run Code Online (Sandbox Code Playgroud) (我是java世界的新手)
我正在学习dropwizard,我想创建返回视图(html)或json的资源,具体取决于请求类型(ajax与否)
例:
@Path("/")
public class ServerResource {
@GET
@Produces(MediaType.TEXT_HTML)
public MainView getMainView() {
return new MainView("Test hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
如果请求是AJAX,如何在相同的Path JSON响应中添加到此资源?
更新1. 我创建了这样的东西:
@Path("/")
public class ServerResource {
@GET
@Consumes(MediaType.TEXT_HTML)
@Produces(MediaType.TEXT_HTML)
public MainView getMainView(@HeaderParam("X-Requested-With") String requestType) {
return new MainView("hello world test!");
}
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<String> getJsonMainView() {
List<String> list = new ArrayList<String>();
for (Integer i = 0; i < 10; i++) {
list.add(i, "test" + i.toString());
}
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
看起来这是按预期工作,但我知道这不是一个好习惯.
因此,我需要创建一个特定的任务,在需要时将通过 POST 请求调用该任务,因此我使用放置向导的任务接口,我创建了一个扩展任务的类,将其注册在 /tasks/ 接口中,然后调用它通过这个电话:
curl -X POST http://localhost:8081/tasks/StoreMigration?maxRows=4&date=2014-02-2T15:23:00Z
Run Code Online (Sandbox Code Playgroud)
无论我放在第一个参数,该参数都会出现在我的任务中,第二个参数会消失。知道如何处理吗?
在使用JDBI的@BindBean将值插入Dropwizard中的Mysql数据库时,我一直得到以下异常.问题似乎是JDBI无法在bean中找到属性.我已经将问题分离到一个单独的项目中,但无法弄清楚这是出错的地方.我会非常感谢你的一些建议.
org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: Unable to execute, no named parameter matches "title" and no positional param for place 0 (which is 1 in the JDBC 'start at 1' scheme) has been set. [statement:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", located:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", rewritten:"/* CarDAO.createCar2 */ INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( ?, ?, ?, unix_timestamp(), ?, …Run Code Online (Sandbox Code Playgroud) Dropwizard 相当新。
我找到了很多处理 Jersey 和 ssl 自签名证书的解决方案。Dropwizard 版本是 0.9.2
我试图设置 SSLContext 但我得到了
The method sslContext(SSLContext) is undefined for the type JerseyClientBuilder
Run Code Online (Sandbox Code Playgroud)
代码:
TrustManager[] certs = new TrustManager[]{
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
};
public static class TrustAllHostNameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
} …Run Code Online (Sandbox Code Playgroud) 在Junit 4我可以做类似的事情
@ClassRule
public DropwizardAppRule<Configuration> app = new DropwizardAppRule<>(MyApp.class);
...
app.getLocalPort()
Run Code Online (Sandbox Code Playgroud)
如何在Junit 5中复制此行为?从这个 github问题看起来我需要使用@ExtendWith(DropwizardExtensionsSupport.class),但它不清楚如何
我们正在运行 DropWizard 并尝试删除导致抛出 404 响应的 URL 的日志记录
我们有一个包罗万象的异常映射器,它接收一个NotFoundException. 令人沮丧的是,该异常没有携带导致它被抛出的 URL 的上下文。
示例应用在这里:https : //github.com/pauldambra/not.found.example
我们正在使用一个 ExceptionMapper
public class NotFoundLogger implements ExceptionMapper<NotFoundException> {
ExampleLogger logger = new ExampleLogger();
@Override
public Response toResponse(final NotFoundException exception) {
logger.error(urlFrom(exception), exception);
return Response.status(404).build();
}
private String urlFrom(final NotFoundException exception) {
return "why is this not a property on the exception?!";
}
private class ExampleLogger {
void error(final String notFoundUrl, final NotFoundException exception) {
System.out.println("someone tried to load " + notFoundUrl);
System.out.println(exception.getMessage());
} …Run Code Online (Sandbox Code Playgroud) 我正在 Dropwizard 应用程序中设置视图,但遇到了一个关于 Freemarker 的奇怪问题。
public class ExampleFreemarkerView extends View {
private Foo foo;
public ContractHtmlView(Foo Foo) {
super("FooView.ftl");
this.foo = foo;
}
public Contract getFoo() { return foo };
}
public class Foo {
public String bar = "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
和 FooView.ftl
<html>
<body>
<h1>${foo.bar}</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
呈现时的预期输出ExampleFreemarkerView是显示 的 HTML 文档Hello World。
实际发生的是 Freemarker 抛出异常,抱怨${foo.bar}- 特别bar是 - 未定义。
这似乎是因为bar是一个公共字段,没有吸气剂。当我向 中添加public String getBar() …
java freemarker public-members dropwizard dropwizard-templates
dropwizard ×10
java ×8
jax-rs ×2
dao ×1
freemarker ×1
jdbi ×1
jersey ×1
jetty ×1
junit-rule ×1
junit5 ×1
mysql ×1
rest ×1
ssl ×1
task ×1
uri ×1