Tag: jersey client

jersey客户端可以使用JSON将JAXB对象发布到服务器吗?

我找到了很多关于如何设置jersey服务器以便它可以生成和使用JAXB绑定对象的示例,但是我很难找到如何让客户端发布相同的JAXB绑定对象的示例。 此示例显示如何使用XML执行此操作 。 我正在寻找一个展示如何使用JSON的方法。 我甚至不确定这是否可行。 post方法的javadoc是不明确的。 我的post看起来像这样: Client client = Client.create(); WebResource resource = client.resource(uri); ClientResponse response = resource.type(MediaType.APPLICATION_JSON) .post(ClientResponse.class, instanceWithXmlRootElementAnnotation); 当我尝试这个时,我的服务器获取请求,但@FormParam的字段总是作为空发送。 这是我的服务器端方法的签名: @POST @Path(“apath”) @Consumes(MediaType.APPLICATION_JSON) public String postAPath(@FormParam(“InstanceWithXmlRootElementAnnotation”) InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) { //instanceWithXmlRootElementAnnotation is always null 我想知道的是,我是否应该使用instanceWithXmlRootElementAnnotation 。 如果这是传统的Web服务,我将使用JAXB 生成一个对象供客户端使用并通过生成的类发送。 但是从我从我链接的示例中收集的内容来看,这个人正在发送源代码,而不是生成的类。

在Jersey Client 2中编码花括号

我们正在使用Jersey Client 2.21。 我注意到,当我们将花括号(又名花括号)作为参数值时,它不会被正确编码。 不仅如此,花括号内的任何内容都不会被编码。 对于我测试过的常规括号或其他不安全字符,情况并非如此。 请参阅下面的示例。 在这个例子中,我输入三个参数。 一个只有空格的控制参数。 一个用花括号,一个用常规括号。 public static void testJerseyEncoding() { Client client = ClientBuilder.newClient(); String url = “http://foo.com/path”; Map map = new HashMap(); map.put(“paramWithCurly”, ” {with a space}”); map.put(“paramWithOutCurly”, “with a space”); map.put(“paramWithBracket”, “[with a space]”); WebTarget target = client.target(url); for (Map.Entry entry : map.entrySet()) { target = target.queryParam(entry.getKey(), entry.getValue()); […]

将Jersey客户端1.x升级到2.x时,UriBuilder上的AbstractMethodError

我们正在将我们的Web应用程序(在Tomcat7上运行)从Jersey 1.7升级到Jersey 2.4.1。 我设法让服务器端正常工作,但是使用jersey-client的JUnit测试抛出了AbstractMethodError:java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder; at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119) at org.glassfish.jersey.client.JerseyWebTarget.(JerseyWebTarget.java:72) at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:180) at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:69) at ******.test.resources.ResourceConfig.getResourceWithCredentialsAt(ResourceConfig.java:144) at ******.test.resources.ResourceConfig.getResourceAsUserAt(ResourceConfig.java:99) at ******.test.resources.UsersResourceTest.testGetAuthorizedUser(UsersResourceTest.java:251) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at […]

如何将列表作为来自jersey2客户端的响应

我试过这个, List list=client.target(url). request(MediaType.APPLICATION_JSON).get(new GenericType<List>(){}); 但我没有得到列表而是我得到空值

Jersey客户端API vs Jersey测试框架

我是Web服务的新手,想知道Jersey客户端API和泽西测试框架有什么区别? 我想测试我的jerseyREST网络服务端点。 哪个是正确的?

使用Jersey客户端的PATCH请求

我想执行我们的服务器支持的PATCH请求,以便使用Jersey客户端进行测试。 我的代码如下,但我得到com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: HTTP method PATCH doesn’t support outputexception。 有人能让我知道下面的代码有什么问题吗? String complete_url = “http://localhost:8080/api/request”; String request = “[{\”op\”:\”add\”, \”path\”:\”/name\”, \”value\”:\”Hello\”}]”; DefaultClientConfig config = new DefaultClientConfig(); config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true); Client client = Client.create(config); WebResource resource = client.resource(complete_url); ClientResponse response = resource.header(“Authorization”, “Basic xyzabCDef”) .type(new MediaType(“application”, “json-patch+json”)) .method(“PATCH”, ClientResponse.class, request); 这是完整的例外, com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: HTTP method PATCH doesn’t support […]

接受并返回对象的REST服务。 怎么写客户端?

我已经声明了两个REST Web服务。 一个只返回一个对象的人。 和其他接受对象并返回另一个对象。 使用POJO Order.java。 @XmlRootElement public class Order { private String id; private String description; public Order() { } @XmlElement public String getId() { return id; } @XmlElement public String getDescription() { return description; } // Other setters and methods } Web服务定义为 @Path(“/orders”) public class OrdersService { // Return the list of orders […]

jersey – StreamingOutput作为响应实体

我在Jersey Resource类中实现了流输出。 @GET @Path(“xxxxx”) @Produces(BulkConstants.TEXT_XML_MEDIA_TYPE}) public Response getFile() { FeedReturnStreamingOutput sout = new FeedReturnStreamingOutput(); response = Response.ok(sout).build(); return response; } class FeedReturnStreamingOutput implements StreamingOutput { public FeedReturnStreamingOutput() @Override public void write(OutputStream outputStream) { //write into Output Stream } } 问题是尽管在调用FeedReturnStreamingOutput之前从资源发回响应但是Jersey客户端等待直到FeedReturnStreamingOutput执行完成。 客户代码: Client client = Client.create(); ClientResponse response = webResource //headers .get(ClientResponse.class); //The codes underneath executes […]

使用Jersey客户端重置连接

我在生产中看到很多连接重置。可能有多种原因,但我想确保代码中没有连接泄漏。我在代码中使用Jersey客户端 Client this.client = ApacheHttpClient.create(); client.resource(“/stores/”+storeId).type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class,indexableStore); 最初我用以下方式实例化客户端客户端this.client = Client.create(),我们将其更改为ApacheHttpClient.create()。 我没有在响应上调用close()但我假设ApacheHttpClient会在内部执行,因为HttpClient executeMethod被调用,它会为我们处理所有的样板。 编写代码的方式是否存在潜在的连接泄漏?

Jersey REST Client:发布MultiPart数据

我正在尝试编写一个Jersey客户端应用程序,它可以将多部分表单数据发布到Restful Jersey服务。 我需要发布带有数据的CSV文件和带有元数据的JSON。 我正在使用Jersey客户端1.18.3。 这是我的代码(一些名称已被更改为公司保密)… Client client = Client.create(); WebResource webResource = client.resource(“http://localhost:8080/mariam/service/playWithDad”); FileDataBodyPart filePart = new FileDataBodyPart(“file”, new File(“C:/Users/Admin/Desktop/input/games.csv”)); String playWithDadMetaJson = “{\n” + ” \”sandboxIndicator\”: true,\n” + ” \”skipBadLines\”: false,\n” + ” \”fileSeparator\”: \”COMMA\”,\n” + ” \”blockSize\”: false,\n” + ” \”gameUUID\”: \”43a004c9-2130-4e75-8fd4-e5fccae31840\”,\n” + ” \”useFriends\”: \”false\”\n” + “}\n” + “”; MultiPart multipartEntity = […]