原文: https://howtodoinjava.com/resteasy/restful-webservices-client-using-java-net-package/
到目前为止,在此博客中,我们已经学习了有关构建服务器端组件 RESTful Web 服务的知识。 在本文中,我们将学习构建一个 RESTful 客户端,以使用先前文章中编写的 Web 服务。
我将重新使用为 RESTEasy + JAXB xml 示例编写的代码库。 我将构建一个不使用任何第三方工具的纯 Java API 客户端。
1)构建 RESTful Web 服务 API
请遵循 RESTEasy + JAXB xml 示例中给出的步骤。
作为参考,服务和模型类为:
UserManagementModule.java
package com.howtodoinjava.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import com.howtodoinjava.model.User;
@Path("/user-management")
public class UserManagementModule
{
@GET
@Path("/users/{id}")
@Produces("application/xml")
public Response getUserById(@PathParam("id") Integer id)
{
User user = new User();
user.setId(id);
user.setFirstName("Lokesh");
user.setLastName("Gupta");
return Response.status(200).entity(user).build();
}
}
User.java
package com.howtodoinjava.model;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@XmlAttribute(name = "id")
private int id;
@XmlElement(name = "firstName")
private String firstName;
@XmlElement(name = "lastName")
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
2)创建 RESTFul Web 服务的用户客户端
我们的 Java 客户端基于java.net
包 API。 我在这里做两个步骤:
- 以字符串格式捕获输出
- 从 xml 响应解组模型对象
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.howtodoinjava.model.User;
public class PureJavaClient
{
public static void main(String[] args)
{
try
{
URL url = new URL("http://localhost:8080/RESTfulDemoApplication/user-management/users/10");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String apiOutput = br.readLine();
System.out.println(apiOutput);
conn.disconnect();
JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
User user = (User) jaxbUnmarshaller.unmarshal(new StringReader(apiOutput));
System.out.println(user.getId());
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="10"><firstName>Lokesh</firstName><lastName>Gupta</lastName></user>
10
Lokesh
Gupta
单击以下链接下载此示例的源代码。
祝您学习愉快!