>本节详细介绍了如何从jax-rs客户端中的邮政请求中读取响应主体。 核心方法涉及使用ClientResponse
方法返回的对象。 该对象提供对响应状态代码和实体(响应主体)的访问。 特定方法取决于响应主体的内容类型。 对于常见的JSON响应,您需要使用JSON处理库(例如Jackson或Gson。 您还需要将杰克逊依赖性包括在项目中。invoke()
WebTarget
有效地解析JSON响应主体
之类的注释将JSON密钥准确地映射到Java字段。
import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; //Jackson library public class ReadPostResponseBody { public static void main(String[] args) { Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://your-api-endpoint.com/your-resource"); // Create a sample request entity (replace with your actual data) String requestBody = "{\"key1\":\"value1\", \"key2\":\"value2\"}"; Response response = target.request(MediaType.APPLICATION_JSON).post(Entity.json(requestBody)); if (response.getStatus() == 200) { //Check for successful response try { ObjectMapper objectMapper = new ObjectMapper(); YourResponseObject responseObject = objectMapper.readValue(response.readEntity(String.class), YourResponseObject.class); //Process responseObject System.out.println("Response: " + responseObject); } catch (Exception e) { e.printStackTrace(); } } else { System.err.println("Request failed with status code: " + response.getStatus()); } response.close(); client.close(); } //Define your response object structure public static class YourResponseObject { // Your class fields here } }
"http://your-api-endpoint.com/your-resource"
强大错误处理的最佳实践至关重要。 始终检查服务器返回的HTTP状态代码。 2XX状态代码表示成功,而其他代码表示错误。 您的代码应以不同的方式处理这些。 例如:YourResponseObject
处理响应主体正常。
objectMapper.readValue()
@JsonProperty
IOException
:网络问题,连接超时。JSONException
ProcessingException
import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; //Jackson library public class ReadPostResponseBody { public static void main(String[] args) { Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://your-api-endpoint.com/your-resource"); // Create a sample request entity (replace with your actual data) String requestBody = "{\"key1\":\"value1\", \"key2\":\"value2\"}"; Response response = target.request(MediaType.APPLICATION_JSON).post(Entity.json(requestBody)); if (response.getStatus() == 200) { //Check for successful response try { ObjectMapper objectMapper = new ObjectMapper(); YourResponseObject responseObject = objectMapper.readValue(response.readEntity(String.class), YourResponseObject.class); //Process responseObject System.out.println("Response: " + responseObject); } catch (Exception e) { e.printStackTrace(); } } else { System.err.println("Request failed with status code: " + response.getStatus()); } response.close(); client.close(); } //Define your response object structure public static class YourResponseObject { // Your class fields here } }
>
以上是从邮政请求中读取jax-rs客户端中的响应主体的详细内容。更多信息请关注PHP中文网其他相关文章!