下面是我用来将 Java 对象作为 XML 返回的代码片段:
@Path("employeeinfo")
@Produces(MediaType.APPLICATION_XML)
public class MyResource {
@GET
public Employee getEmployee(@Context Request r) {
Employee emp = new Employee();
emp.setName("Name");
emp.setAge(30);
emp.setPlace("place");
return emp;
}
Employee 类是:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name= "emp")
public class Employee {
public Employee() {}
private String name;
private int age;
private String place;
}
POM xml 是
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rest.learning</groupId>
<artifactId>RestWebservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>RestWebservice</name>
<build>
<finalName>RestWebservice</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
就设置而言一切都很好,但是当我评估 Postman 的服务时出现错误
"SEVERE: MessageBodyWriter not found for media type=application/xml, type=class rest.learning.Employee, genericType=class rest.learning.Employee."
我无法在这里检测到错误。我搜索了所有互联网但没有找到任何答案