You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "arturobernalg (via GitHub)" <gi...@apache.org> on 2026/03/28 16:42:15 UTC
[PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
arturobernalg opened a new pull request, #816:
URL: https://github.com/apache/httpcomponents-client/pull/816
This PR adds httpclient5-jakarta-rest-client, a small Java 11+ client-side module that creates implementations of jakarta.ws.rs-annotated interfaces with Apache HttpClient underneath.
The c/]qurrent implementation is intentionally minimal and focused on the client side only.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4304698647
> @ok2c I removed the dependency on it in the Jakarta REST client and switched the POJO request body path to the current Jackson2 producer API. I also disabled the test that was asserting the custom JSON media type corner case for POJO bodies.
@arturobernalg Great! I will do a thorough review either this weekend or the next one the latest. This is _the_ top priority change-set for me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4226310331
@ok2c
I reworked the patch to keep it small and direct, dropped the extra framework-like pieces, and aligned it more closely with the existing HttpClient style.
Is this closer to what you had in mind when you said it needed to be done better, or do you still see parts that feel off in terms of scope or design?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg merged PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4325382349
> @arturobernalg Could we use httpbin as a target for the example code to simplify it?
>
> `curl -X GET "https://httpbin.org/get" -H "accept: application/json"`
>
> ```
> {
> "args": {},
> "headers": {
> "Accept": "application/json",
> "Accept-Encoding": "gzip, deflate, br, zstd",
> "Accept-Language": "en-US,en;q=0.5",
> "Host": "httpbin.org",
> "Priority": "u=0",
> "Referer": "https://httpbin.org/",
> "Sec-Fetch-Dest": "empty",
> "Sec-Fetch-Mode": "cors",
> "Sec-Fetch-Site": "same-origin",
> "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0",
> "X-Amzn-Trace-Id": "Root=1-69edec00-494d54b55f97010c2add55f6"
> },
> "origin": "213.55.197.162",
> "url": "https://httpbin.org/get"
> }
> ```
>
> Looks good otherwise. I would like to try to re-work some of the internals and the exception handling but this can be done as a follow-up change.
@ok2c please check again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4334856613
@ok2c I'll add the doc latter on.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4321846935
@arturobernalg Could we use httpbin as a target for the example code to simplify it?
`curl -X GET "https://httpbin.org/get" -H "accept: application/json"`
```
{
"args": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.5",
"Host": "httpbin.org",
"Priority": "u=0",
"Referer": "https://httpbin.org/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0",
"X-Amzn-Trace-Id": "Root=1-69edec00-494d54b55f97010c2add55f6"
},
"origin": "213.55.197.162",
"url": "https://httpbin.org/get"
}
```
Looks good otherwise. I would like to try to re-work some of the internals and the exception handling but this can be done as a follow-up change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4226469705
@arturobernalg
1. Please use the async client because we want the REST client to support HTTP/2 as well. Please use it _without_ resorting to content buffering in memory (do not use 'simple' message objects). 'Simple' APIs are not intended for production use, so we should not be using them internally. Use the new Jackson2 bindings from core.
2. Please do not re-implement the percent codec. What stops you from using what we already have? Not good enough? Improve it.
3. Please use `URIBuilder` to construct request URIs.
The whole thing still looks like it just got vibe-coded with some AI agent. There is no need to rush it. Take your time to do it right. I think this can be the most important contribution to the project since @rschmitt reactive module. So, _please_ take your time
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4304509563
> > I haven't forgot of the PR. I just waiting for this change [54135f6](https://github.com/apache/httpcomponents-core/commit/54135f6cacfeb28208c58b4dadd355f6b5a2dfad)
>
> @arturobernalg I do not understand how this can be holding you back. This change in core allows for a custom content-type for JSON content, which looks like a corner case to me. I do not see how it is relevant here.
@ok2c
I removed the dependency on it in the Jakarta REST client and switched the POJO request body path to the current Jackson2 producer API. I also disabled the test that was asserting the custom JSON media type corner case for POJO bodies.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#discussion_r3005035721
##########
httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/examples/RestClientExample.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.rest.RestClientBuilder;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates how to use {@link RestClientBuilder}
+ * to create a type-safe REST client from a Jakarta REST annotated
+ * interface. The proxy translates method calls into HTTP requests
+ * executed through Apache HttpClient.
+ */
+public class RestClientExample {
+
+ // -- Domain object --
+
+ public static class User {
+ public int id;
+ public String name;
+ public String email;
+
+ public User() {
+ }
+
+ public User(final int id, final String name,
+ final String email) {
+ this.id = id;
+ this.name = name;
+ this.email = email;
+ }
+
+ @Override
+ public String toString() {
+ return "User{id=" + id
+ + ", name='" + name + '\''
+ + ", email='" + email + '\'' + '}';
+ }
+ }
+
+ // -- Jakarta REST annotated interface --
+
+ @Path("/api/users")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public interface UserApi {
+
+ @GET
+ List<User> list();
+
+ @GET
+ @Path("/{id}")
+ User get(@PathParam("id") int id);
+
+ @GET
+ @Path("/search")
+ @Produces(MediaType.APPLICATION_JSON)
+ List<User> search(@QueryParam("name") String name);
+
+ @POST
+ User create(User user);
+
+ @PUT
+ @Path("/{id}")
+ User update(@PathParam("id") int id, User user);
+
+ @DELETE
+ @Path("/{id}")
+ void delete(@PathParam("id") int id);
+ }
+
+ public static void main(final String[] args) throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+
+ // Start an embedded HTTP server for demonstration
+ final HttpServer server = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("localhost")
+ .register("/api/users", new HttpRequestHandler() {
+ @Override
+ public void handle(
+ final ClassicHttpRequest request,
+ final ClassicHttpResponse response,
+ final HttpContext context)
+ throws HttpException, IOException {
+ final String method = request.getMethod();
+ if ("GET".equals(method)) {
+ final User[] users = {
+ new User(1, "Alice",
+ "alice@example.com"),
+ new User(2, "Bob",
+ "bob@example.com")
+ };
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(users), ContentType.APPLICATION_JSON));
+ } else if ("POST".equals(method)) {
+ final String body = EntityUtils.toString(request.getEntity());
+ final User u = mapper.readValue(body, User.class);
+ u.id = 3;
+ response.setCode(201);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
+ }
+ }
+ })
+ .register("/api/users/*",
+ new HttpRequestHandler() {
+ @Override
+ public void handle(
+ final ClassicHttpRequest request,
+ final ClassicHttpResponse response,
+ final HttpContext context)
+ throws HttpException, IOException {
+ final String path = request.getRequestUri();
+ final String method = request.getMethod();
+
+ if (path.contains("/search")) {
+ final String uri = request.getRequestUri();
+ final int qi = uri.indexOf("name=");
+ final String name = qi >= 0 ? uri.substring(qi + 5) : "";
+ final User[] results = {
+ new User(1, name, name.toLowerCase() + "@example.com")
+ };
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(results), ContentType.APPLICATION_JSON));
+ return;
+ }
+
+ final String idStr = path.substring(
+ path.lastIndexOf('/') + 1);
+ final int id = Integer.parseInt(idStr);
+
+ if ("GET".equals(method)) {
+ final User u = new User(id, "User-" + id, "user" + id + "@example.com");
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
+ } else if ("PUT".equals(method)) {
+ final String body = EntityUtils.toString(request.getEntity());
+ final User u = mapper.readValue(body, User.class);
+ u.id = id;
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
Review Comment:
## Cross-site scripting
Cross-site scripting vulnerability due to a [user-provided value](1).
[Show more details](https://github.com/apache/httpcomponents-client/security/code-scanning/21)
##########
httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/examples/RestClientExample.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.rest.RestClientBuilder;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates how to use {@link RestClientBuilder}
+ * to create a type-safe REST client from a Jakarta REST annotated
+ * interface. The proxy translates method calls into HTTP requests
+ * executed through Apache HttpClient.
+ */
+public class RestClientExample {
+
+ // -- Domain object --
+
+ public static class User {
+ public int id;
+ public String name;
+ public String email;
+
+ public User() {
+ }
+
+ public User(final int id, final String name,
+ final String email) {
+ this.id = id;
+ this.name = name;
+ this.email = email;
+ }
+
+ @Override
+ public String toString() {
+ return "User{id=" + id
+ + ", name='" + name + '\''
+ + ", email='" + email + '\'' + '}';
+ }
+ }
+
+ // -- Jakarta REST annotated interface --
+
+ @Path("/api/users")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public interface UserApi {
+
+ @GET
+ List<User> list();
+
+ @GET
+ @Path("/{id}")
+ User get(@PathParam("id") int id);
+
+ @GET
+ @Path("/search")
+ @Produces(MediaType.APPLICATION_JSON)
+ List<User> search(@QueryParam("name") String name);
+
+ @POST
+ User create(User user);
+
+ @PUT
+ @Path("/{id}")
+ User update(@PathParam("id") int id, User user);
+
+ @DELETE
+ @Path("/{id}")
+ void delete(@PathParam("id") int id);
+ }
+
+ public static void main(final String[] args) throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+
+ // Start an embedded HTTP server for demonstration
+ final HttpServer server = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("localhost")
+ .register("/api/users", new HttpRequestHandler() {
+ @Override
+ public void handle(
+ final ClassicHttpRequest request,
+ final ClassicHttpResponse response,
+ final HttpContext context)
+ throws HttpException, IOException {
+ final String method = request.getMethod();
+ if ("GET".equals(method)) {
+ final User[] users = {
+ new User(1, "Alice",
+ "alice@example.com"),
+ new User(2, "Bob",
+ "bob@example.com")
+ };
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(users), ContentType.APPLICATION_JSON));
+ } else if ("POST".equals(method)) {
+ final String body = EntityUtils.toString(request.getEntity());
+ final User u = mapper.readValue(body, User.class);
+ u.id = 3;
+ response.setCode(201);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
Review Comment:
## Cross-site scripting
Cross-site scripting vulnerability due to a [user-provided value](1).
[Show more details](https://github.com/apache/httpcomponents-client/security/code-scanning/20)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on code in PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#discussion_r3005038283
##########
httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/examples/RestClientExample.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.io.IOException;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.client5.http.rest.RestClientBuilder;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+/**
+ * This example demonstrates how to use {@link RestClientBuilder}
+ * to create a type-safe REST client from a Jakarta REST annotated
+ * interface. The proxy translates method calls into HTTP requests
+ * executed through Apache HttpClient.
+ */
+public class RestClientExample {
+
+ // -- Domain object --
+
+ public static class User {
+ public int id;
+ public String name;
+ public String email;
+
+ public User() {
+ }
+
+ public User(final int id, final String name,
+ final String email) {
+ this.id = id;
+ this.name = name;
+ this.email = email;
+ }
+
+ @Override
+ public String toString() {
+ return "User{id=" + id
+ + ", name='" + name + '\''
+ + ", email='" + email + '\'' + '}';
+ }
+ }
+
+ // -- Jakarta REST annotated interface --
+
+ @Path("/api/users")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public interface UserApi {
+
+ @GET
+ List<User> list();
+
+ @GET
+ @Path("/{id}")
+ User get(@PathParam("id") int id);
+
+ @GET
+ @Path("/search")
+ @Produces(MediaType.APPLICATION_JSON)
+ List<User> search(@QueryParam("name") String name);
+
+ @POST
+ User create(User user);
+
+ @PUT
+ @Path("/{id}")
+ User update(@PathParam("id") int id, User user);
+
+ @DELETE
+ @Path("/{id}")
+ void delete(@PathParam("id") int id);
+ }
+
+ public static void main(final String[] args) throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+
+ // Start an embedded HTTP server for demonstration
+ final HttpServer server = ServerBootstrap.bootstrap()
+ .setCanonicalHostName("localhost")
+ .register("/api/users", new HttpRequestHandler() {
+ @Override
+ public void handle(
+ final ClassicHttpRequest request,
+ final ClassicHttpResponse response,
+ final HttpContext context)
+ throws HttpException, IOException {
+ final String method = request.getMethod();
+ if ("GET".equals(method)) {
+ final User[] users = {
+ new User(1, "Alice",
+ "alice@example.com"),
+ new User(2, "Bob",
+ "bob@example.com")
+ };
+ response.setCode(200);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(users), ContentType.APPLICATION_JSON));
+ } else if ("POST".equals(method)) {
+ final String body = EntityUtils.toString(request.getEntity());
+ final User u = mapper.readValue(body, User.class);
+ u.id = 3;
+ response.setCode(201);
+ response.setEntity(new StringEntity(mapper.writeValueAsString(u), ContentType.APPLICATION_JSON));
Review Comment:
mmmmm
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4186791813
@arturobernalg Was this change-set AI generated? I have an impression it was authored by someone who has no familiarity with our code base.
This is a start and a good proof of concept. Would you actually be willing to take your time and do it better?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4197373830
> Was this change-set AI generated? I have an impression it was authored by someone who has no familiarity with our code base.
>
> This is a start and a good proof of concept. Would you actually be willing to take your time and do it better?
@ok2c Yes. I can rework it. If you have a specific direction in mind, please let me know
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "ok2c (via GitHub)" <gi...@apache.org>.
ok2c commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4302425082
> I haven't forgot of the PR. I just waiting for this change [54135f6](https://github.com/apache/httpcomponents-core/commit/54135f6cacfeb28208c58b4dadd355f6b5a2dfad)
@arturobernalg I do not understand how this can be holding you back. This change in core allows for a custom content-type for JSON content, which looks like a corner case to me. I do not see how it is relevant here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
Re: [PR] Add Jakarta REST client module for HttpClient [httpcomponents-client]
Posted by "arturobernalg (via GitHub)" <gi...@apache.org>.
arturobernalg commented on PR #816:
URL: https://github.com/apache/httpcomponents-client/pull/816#issuecomment-4302248625
I haven't forgot of the PR. I just waiting for this change [54135f6](https://github.com/apache/httpcomponents-core/commit/54135f6cacfeb28208c58b4dadd355f6b5a2dfad)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org