如何在Jersey RestAPI中读取POST请求的application/xml数据
嘿,我来帮你搞定这个Jersey REST API的XML接收问题!首先先纠正一个小细节:你提到的@Consumes(MediaType.APPLICATION_XHTML_XML)其实不对哦,你需要接收的是application/xml类型,所以应该用@Consumes(MediaType.APPLICATION_XML)。接下来我给你两种常用的读取XML内容的方式,按需选择就行。
方式一:用JAXB自动绑定到Java对象(推荐)
这种方式最简洁,Jersey会自动帮你把XML请求体解析成对应的Java对象,不用手动处理XML流。
步骤1:定义与XML结构对应的Java实体类
用JAXB注解把XML节点和Java字段做映射。假设你要接收的XML结构是这样的:
<employee> <id>101</id> <name>Sudeshna Bhattacharya</name> <department>Software Development</department> </employee>
对应的实体类代码:
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlElement; // 注解指定XML的根节点名称 @XmlRootElement(name = "employee") public class Employee { // 必须保留无参构造方法,JAXB需要它来实例化对象 public Employee() {} private int id; private String name; private String department; // 用@XmlElement指定对应XML子节点的名称 @XmlElement(name = "id") public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(name = "department") public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } }
步骤2:编写Jersey资源类
直接把实体类作为POST方法的参数,Jersey会自动完成XML到对象的转换:
import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/company") public class EmployeeResource { @POST @Path("/employee") // 指定接收application/xml类型的请求 @Consumes(MediaType.APPLICATION_XML) public Response saveEmployee(Employee employee) { // 现在你可以直接操作employee对象了,比如打印数据或者存库 System.out.println("Received employee: " + employee.getName() + " (ID: " + employee.getId() + ")"); // 这里写你的业务逻辑,比如保存到数据库 // ... // 返回创建成功的响应 return Response.status(Response.Status.CREATED) .entity("Employee " + employee.getName() + " added successfully") .build(); } }
依赖说明
如果用Maven管理项目,需要确保引入Jersey的JAXB支持依赖(Jersey 2.x默认可能包含,但保险起见可以手动添加):
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-jaxb</artifactId> <version>2.41</version> <!-- 替换成你的Jersey版本 --> </dependency>
方式二:手动读取并解析XML流
如果你的XML结构不固定,或者不想用JAXB,可以直接读取请求的输入流,自己用DOM/SAX/StAX解析。
示例代码:
import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import java.io.InputStream; @Path("/company") public class EmployeeResource { @POST @Path("/employee") @Consumes(MediaType.APPLICATION_XML) public Response saveEmployee(InputStream inputStream) { try { // 用DOM解析XML输入流 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(inputStream); // 提取XML中的数据 Element root = doc.getDocumentElement(); String id = root.getElementsByTagName("id").item(0).getTextContent(); String name = root.getElementsByTagName("name").item(0).getTextContent(); String department = root.getElementsByTagName("department").item(0).getTextContent(); System.out.println("Received employee: Name=" + name + ", ID=" + id + ", Department=" + department); // 业务逻辑处理... return Response.status(Response.Status.CREATED).build(); } catch (Exception e) { e.printStackTrace(); // 返回XML格式错误的响应 return Response.status(Response.Status.BAD_REQUEST) .entity("Invalid XML format") .build(); } } }
测试接口
你可以用curl命令测试这个接口:
curl -X POST -H "Content-Type: application/xml" -d "<employee><id>101</id><name>Sudeshna Bhattacharya</name><department>Software Development</department></employee>" http://your-site/company/employee
内容的提问来源于stack exchange,提问作者Sudeshna Bhattacharya
相关产品推荐
相关产品推荐

