Tag: jersey

Grizzly和Jersey独立jar子

我正在尝试使用Maven shade插件将Grizzly与Jersey打包成一个单独的jar。 但我总是得到消息No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler 代码在Eclipse中运行良好,但不在打包的jar中: public class Main { private static URI getBaseURI() { return UriBuilder.fromUri(“http://localhost/”).port(9998).build(); } public static final URI BASE_URI = getBaseURI(); protected static HttpServer startServer() throws IOException { System.out.println(“Starting grizzly…”); ResourceConfig rc = new PackagesResourceConfig(“share.test”); rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); return GrizzlyServerFactory.createHttpServer(BASE_URI, rc); } public static void main(String[] args) […]

如何在泽西设置默认为json而不是xml?

使用jersey jersey.java.net如果URI中没有accept标头或.xml后缀,如何将JSON设置为默认序列化而不是XML?

使用Java中的Rest API进行文件上载

我是REST API的新手。 我想使用REST API将用户选择的文件上传到用户提供的路径(远程或本地路径)。 我的html文件有1个文本框和1个文件选择器。 用户将在文本框中输入FilePath(本地或远程计算机文件夹位置)。 请提出如何解决此问题的建议。 这是我的代码: FileUpload.html :: Select a file : Target Upload Path : UploadFileService.java @Path(“/file”) public class UploadFileService { @POST @Path(“/upload”) @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam(“file”) InputStream uploadedInputStream, @FormDataParam(“file”) FormDataContentDisposition fileDetail, @FormParam(“path”) String path) { /*String uploadedFileLocation = “d://uploaded/” + fileDetail.getFileName();*/ /*String uploadedFileLocation = //10.217.14.88/Installables/uploaded/” + fileDetail.getFileName();*/ String uploadedFileLocation = […]

NoSuchMethodError:Jersey客户端中的MultivaluedMap.addAll

我正在尝试使用Jersey Client来模拟对我的Web服务的HTTP请求。 我试图从文档中实现这个简单的例子 。 这是我的简短代码: public void restoreTest(String sessionId) throws Exception { Client client = ClientBuilder.newClient(); WebTarget target = client.target(idsUrl).path(“restore”); Form form = new Form(); form.param(“sessionId”, sessionId); target.request(MediaType.APPLICATION_FORM_URLENCODED_TYPE); } 我甚至没有实现整个示例,因为目前我在最后一行得到了一个exception: java.lang.NoSuchMethodError: javax.ws.rs.core.MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V at org.glassfish.jersey.client.ClientRequest.accept(ClientRequest.java:254) at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:232) at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:60) at org.icatproject.idsclient.TestingClient.restoreTest(TestingClient.java:112) at org.icatproject.ids.ids2.ArchiveTest.restoreThenArchiveDataset(ArchiveTest.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) […]

@Context对象来自哪里

我一直在寻找,但似乎无法找到明确的答案…… 服务器(我的问题的玻璃鱼)注入用@Context注释的实际objets的机制是什么? 更具体地说,如果我想编写一个类似于以下内容的类: @Path(“/”) public class MyResource { @GET public String doSomething(@Context MyObject obj) { // … } } 那我该怎么办? MyObject在哪里实现,谁在做,以及如何实现? 编辑:我见过以下内容: 在JAX-RS中使用@ Context,@ Provider和ContextResolver http://jersey.576304.n2.nabble.com/ContextResolver-confusion-td5654154.html 但是,这与我所看到的不一致,例如在org.neo4j.server.rest.web.RestfulGraphDatabase的构造函数中,它具有以下签名: public RestfulGraphDatabase( @Context UriInfo uriInfo, @Context Database database, @Context InputFormat input, @Context OutputFormat output, @Context LeaseManager leaseManager )

泽西请求仅对特定URI进行过滤

我正在尝试使用ContainerRequestFilter对进入我服务的请求进行一些validation。 一切都运行正常,但是有一个问题 – 每个请求都会通过filter,即使某些filter永远不会应用于它们(一个filter只在ResourceOne上validation,另一个只在ResourceTwo等上validation) 有没有办法在某些条件下设置仅在请求上调用filter? 虽然它不是阻碍或阻碍,但能够阻止这种行为会很好:)

如何在Jersey调用之前获取与URI匹配的资源方法?

我正在尝试实现一个ContainerRequestFilter ,它对请求的参数进行自定义validation。 我需要查找将与URI匹配的资源方法,以便我可以从方法的参数中删除自定义注释。 根据这个答案,我应该能够注入ExtendedUriInfo ,然后使用它来匹配方法: public final class MyRequestFilter implements ContainerRequestFilter { @Context private ExtendedUriInfo uriInfo; @Override public ContainerRequest filter(ContainerRequest containerRequest) { System.out.println(uriInfo.getMatchedMethod()); return containerRequest; } } 但getMatchedMethod显然会返回null ,一直到实际调用该方法为止(此时对我来说进行validation为时已晚)。 在调用资源方法之前,如何检索将与给定URI匹配的方法? 对于那些感兴趣的人,我正在尝试滚动我自己的必需参数validation,如JERSEY-351中所述 。

在Content-Type中指定charset时,Jersey和@FormParam无法正常工作

看起来Jersey 2.0(使用servlet 3.1)在Content-Type标头中指定charset属性时无法解码参数。 例如,考虑以下端点: @POST @Path(“/hello”) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Response hello(@FormParam(“name”) String name) { System.out.println(name); return ok(); } 此curl请求有效: curl -X POST -H “content-type: application/x-www-form-urlencoded” -d “name=tom” http://localhost:8080/sampleapp/hello 相反,以下请求不起作用, name参数为null : curl -X POST -H “content-type: application/x-www-form-urlencoded; charset=UTF-8” -d “name=tom” http://localhost:8080/sampleapp/hello 我认为内容类型中的charset=UTF-8添加会破坏我的代码。 编辑: 我打开了官方机票以防这是一个错误: https : //java.net/jira/browse/JERSEY-1978

如何通过RESTful Web服务正确生成JSON?

我是第一次写网络服务。 我创建了一个基于Jersey的RESTful Web服务。 我想制作JSON 。 如何生成正确的JSON类型的Web服务需要做什么? 这是我的一个方法: @GET @Path(“/friends”) @Produces(“application/json”) public String getFriends() { return “{‘friends’: [‘Michael’, ‘Tom’, ‘Daniel’, ‘John’, ‘Nick’]}”; } 我只是为我的方法指出@Produces(“application/json”)注释就足够了吗? 那么这个方法可能会返回任何类型的对象? 还是只有String? 我是否需要对这些对象进行额外处理或转换? 请帮助我作为初学者来处理这些问题。 提前致谢!

java Jersey 2.1客户端线程安全吗?

针对jersey 2.0的文档说 : 客户端实例是昂贵的资源。 建议重新使用已配置的实例来创建Web资源。 Web资源的创建,请求的构建和响应的接收都保证是线程安全的。 因此,可以在多个线程之间共享Client实例和WebResource实例 客户端在2.1版中仍然是线程安全的吗? 我在2.1的文档中找不到有关线程安全的信息。