我知道这听起来像是this或其他一些内容的重复,但请耐心听我说。
我有一个非常基本的 JAX-RS 资源,添加了我在本教程中看到的所有必需注释,我遵循here。
但我不断收到
HTTP Status 500
以及 Eclipse 控制台中的以下日志输出。
Mar 18, 2021 1:35:23 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/xml, type=class com.varun.demorest.model.User, genericType=class com.varun.demorest.model.User.
使用 Maven,但即使在添加了我在类似问题上找到的大多数建议后,我发现它大部分已经包含在
下<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
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>com.varun</groupId>
<artifactId>demorest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demorest</name>
<build>
<finalName>demorest</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-moxy</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
我的模特班:
用户.java:
package com.varun.demorest.model;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private String name;
private String phone;
public User() {
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
自定义资源.java:
package com.varun.demorest;
import com.varun.demorest.model.User;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("getUser")
public class CustomResource {
@GET
@Produces(MediaType.APPLICATION_XML)
public User getUser() {
System.out.println("getUser Called!");
User user = new User();
user.setName("Varun");
user.setPhone("xxxxxxxxxx");
System.out.println(user);
return user;
}
}
我在 JAX-RS 方面没有经验,因此非常感谢任何帮助。
使用 Java 11 和 Tomcat 10。
正如 @Paul Samsotha 所说,它为我解决了这个问题,我在 pom.xml 中附加了以下内容:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.2</version>
</dependency>
它解决了问题。
我遵循相同的教程并遇到了相同的问题,我通过添加以下依赖项修复了它:
` <dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.2</version>
</dependency>
`
我使用 JDK 17 和 Tomcat 10,面临同样的问题。添加以下依赖项有助于我启用 JAXB,而不会出现任何错误。 (如果你没有这些 - 就会出现一些或其他错误)
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.0</version>
</dependency>
在 Eclipse 2022-03 中运行类似的代码,遇到了相同的错误 用这三个依赖项修复了它
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.2</version>
</dependency>
在我能够返回 Xml 数据之前, 我想知道为什么这些有效
我使用了 jaxb-runtime 和 jersey-media-jaxb jar 修复了错误。
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.0-M4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.4</version>
</dependency>
在 pojo 类中添加了 @jakarta.xml.bind.annotation.XmlRootElement。 并在 pom.xml 中添加以下依赖项
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.0-M4</version>
</dependency>
此问题已解决。
不要使用
@XmlRootElement
,而使用 @jakarta.xml.bind.annotation.XmlRootElement
这是一个很愚蠢的事情,解决了我的问题。这太令人沮丧了,我花了几个小时来解决这个问题,这个可怕的小东西解决了它。请确认我的回答是否对您有帮助。谢谢。
如果您需要 JAXB 序列化(从测试角度来看也很好):
MessageBodyWriter
接口:@Provider
@Produces("application/xml")
public class MyDataJaxbMessageBodyWriter implements MessageBodyWriter<MyData> {
@Override
public boolean isWriteable(
Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return aClass == MyData.class;
}
@Override
public long getSize(MyData data, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
// deprecated by JAX-RS 2.0 and ignored by Jersey runtime
return 0;
}
@Override
public void writeTo(
MyData data,
Class<?> aClass,
Type type,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> multivaluedMap,
OutputStream outputStream)
throws WebApplicationException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(MyData.class);
jaxbContext.createMarshaller().marshal(data, outputStream);
} catch (JAXBException jaxbException) {
throw new RuntimeException("Error serializing a MyData.class to the output stream", jaxbException);
}
}
}
注意:确保您已使用以下方式注释您的实现:
@Provider
@Produces("application/xml")
environment.jersey().register(MyDataJaxbMessageBodyWriter.class);