我正在尝试使用Restlet发送一个byte [](使用PUT),但我找不到有关如何执行此操作的任何信息.我的代码看起来像这样:
Request request = new Request(Method.PUT, url);
request.setEntity( WHAT DO I PUT HERE?, MediaType.APPLICATION_OCTET_STREAM);
Run Code Online (Sandbox Code Playgroud)
我曾期望找到类似于ByteArrayRepresentation的东西,就像有一个JsonRepresentation和一个StringRepresentation但我找不到任何东西.
我有一个用Restlet库实现的JAX-RS Web服务,现在我想测试它.为了做到这一点,我想通过使用模拟服务预先初始化我的测试来托管这项服务.
托管此类服务和执行测试调用的最佳方法是什么?
@Path("/srv")
public class MyService
{
@GET
public void action(@Context UriInfo uri)
{ ... }
}
@Test
public void myTest()
{
MyService service = new MyService();
service.setSomething(...);
// How do I host it?
// How do I call it?
}
Run Code Online (Sandbox Code Playgroud) 我在网上搜索了一段时间,几乎所有关于使用restlet提供图像的问题都是静态图像.我想要做的是从restlet提供动态生成的图像.
我尝试使用restlet提供静态图像,它正在工作.此外,我可以成功生成动态图像并将其存储在本地文件夹中,因此问题在于如何提供它.如果它是一个http响应,我要做的是将图像的所有字节附加到响应的主体.但是,我不确定如何使用restlet来做到这一点?是FileRepresentation吗?
这个领域的新手,任何建议都将受到欢迎.
谢谢
我想使用具有细粒度身份验证的RESTlet公开资源.我ServerResource
应该只能通过GET
身份验证的成员访问(使用BASIC身份验证).但是,POST
对于没有任何身份验证的呼叫者,也应该可以使用请求.
为了清除:
http:// path/myapp/user应该允许任何人注册使用POST
,但只有注册的成员才能够GET
拥有所有用户的列表.
遗憾的是,我很少进入RESTlet,我只找到使用粗略身份验证的整个Restlet
s或Router
s的示例.
那么如何为资源启用可选身份验证并在每个方法级别上检查它们?
提前致谢!
我正在尝试在RESTlet 文档上运行第一个服务器教程,但即使我将jar添加到我的类路径中,我也会收到错误.我添加了org.restlet.jar和org.restlet.ext.*
服务器的代码是:
package test;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
public class Test extends ServerResource {
public static void main(String[] args) throws Exception {
// Create the HTTP server and listen on port 8182
new Server(Protocol.HTTP, 8182, Test.class).start();
}
@Get
public String toString() {
return "hello, world";
}
}
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是:
11/01/2011 1:22:08 PM org.restlet.engine.Engine registerHelper
INFO: Unable to register the helper org.restlet.ext.httpclient.HttpClientHelper
java.lang.NoClassDefFoundError: org/apache/http/conn/scheme/SocketFactory
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.getConstructor(Class.java:1657)
at org.restlet.engine.Engine.registerHelper(Engine.java:716)
at …
Run Code Online (Sandbox Code Playgroud) 我的应用程序抛出NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch
错误.不知道为什么,因为16.0.1确实包含那个类,我已经检查了.根据我的研究,看起来这是一个错误?
我也有这个代码用于引用,但我认为这不是问题:
FirewallRule rule = new PeriodicFirewallCounterRule(60, TimeUnit.SECONDS, new IpAddressCountingPolicy());
((PeriodicFirewallCounterRule)rule).addHandler(new RateLimitationHandler(new UniqueLimitPolicy(10)));
FirewallFilter firewallFiler = new FirewallFilter(getContext(), list(rule));
firewallFiler.setNext(ma);
Run Code Online (Sandbox Code Playgroud)
我的应用程序正在使用Restlet APISpark:
<dependency>
<groupId>org.restlet.gae</groupId>
<artifactId>org.restlet.ext.apispark</artifactId>
<version>${version.restlet}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
运行并访问应用程序的REST API时,会抛出此错误:
[INFO] Caused by: java.lang.NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch;
[INFO] at org.restlet.ext.apispark.internal.firewall.rule.counter.PeriodicCounter.<init>(PeriodicCounter.java:65)
[INFO] at org.restlet.ext.apispark.internal.firewall.rule.PeriodicFirewallCounterRule$1.load(PeriodicFirewallCounterRule.java:86)
[INFO] at org.restlet.ext.apispark.internal.firewall.rule.PeriodicFirewallCounterRule$1.load(PeriodicFirewallCounterRule.java:84)
[INFO] at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3599)
[INFO] at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2379)
[INFO] at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2342)
[INFO] at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
[INFO] ... 74 more
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Restlet的ClientResource将HTTPS连接到使用自签名证书的服务器.我使用一个独立的应用程序,只使用ClientResource,我的自定义SSLContextFactory添加为属性,代码可以在这里看到:
https://github.com/pixelatedpete/selfsignedexample
当我在一个更复杂的Restlet应用程序(使用与上面相同的pom)中使用相同的类(DynamicTrustManager和SelfSignSslSocketFactory)时,使用Restlet提供通过Jetty提供的REST API,我的自定义SSLContextFactory不再被使用.
我将它添加到ClientResource上下文中,但我从未看到任何日志消息表明提供给ClientResource的SSLContextFactory传递给底层的httpclient.
如果我直接使用HttpClient而不是ClientResource重写:
HttpPost post = new HttpPost(cr.getReference().toString());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(...);
DynamicTrustManager tm = new DynamicTrustManager(..., cert);
SelfSignTrustSslContextFactory scf = (SelfSignTrustSslContextFactory)
CloseableHttpClient httpclient = HttpClients.custom().setSslcontext(scf.createSslContext()).build();
CloseableHttpResponse response = httpclient.execute(post);
Run Code Online (Sandbox Code Playgroud)
事情再次奏效.
这是否是其他任何人遇到过的事情,并且可以指出我怀疑是一件非常明显的事我错过了什么?
铌.使用Tomcat再次尝试并获得相同的问题
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Run Code Online (Sandbox Code Playgroud)
还尝试注入SslContextFactory(我们在这里使用Guice),但这也没有帮助.
如何将自己的标题添加到ClientResource
Restlet 包装的请求中?例如,我已经读过您在直接使用时可以使用以下内容Client
:
Form headers = (Form) request.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
if (headers == null) {
headers = new Form();
request.getAttributes().put("org.restlet.http.headers", responseHeaders);
}
headers.add("X-Some-Header", "the value");
Run Code Online (Sandbox Code Playgroud)
但是,我基本上遵循他们的教程中提供的代码,我不知道ClientResource
应该访问哪个成员来设置标头:
ClientResource clientResource = new ClientResource("http://webserviceurl");
MyClassResource classResource = clientResource.wrap(classResource.class);
MyClass class;
try { class = resource.retrieve(); } catch (Exception e) { System.out.println("fail."); }
Run Code Online (Sandbox Code Playgroud)
如何修改retrieve()
以添加一些标题?
如何在restlet android中添加Header params?
我有以下代码:
ClientResource clientResource = null;
try {
clientResource = new ClientResource(jsonRestHelper.getUrl());
for (Parameter parameter : jsonRestHelper.getParameters()) {
//here works perfectly, jsonRestHelper is a helper class
clientResource.addQueryParameter(parameter);
}
//here I wanna add headers param example
//addHeader("Key","Value");
} catch (Exception e) {}
Run Code Online (Sandbox Code Playgroud)