严重:找不到媒体类型 = application/xml 的 MessageBodyWriter

Var*_*nde 8 jax-rs jersey

我知道这听起来可能像这个或其他一些的重复,但请耐心听我说。

我有一个非常基本的 JAX-RS 资源,添加了我在本教程中看到的所有必需注释,我在此处遵循。
但我不断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.
Run Code Online (Sandbox Code Playgroud)

使用 Maven,但即使在添加了我在类似问题上找到的大多数建议后,我发现它大部分已经包含在

<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我的模型类:
User.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

自定义资源.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我对 JAX-RS 缺乏经验,因此非常感谢任何帮助。
使用 Java 11 和 Tomcat 10。

小智 6

正如 @Paul Samsotha 所说,它为我解决了这个问题,我在 pom.xml 中附加了以下内容:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

它解决了问题。