【问题标题】:SEVERE: MessageBodyWriter not found for media type=application/xml严重:找不到媒体类型 = 应用程序/xml 的 MessageBodyWriter
【发布时间】:2021-06-15 05:35:30
【问题描述】:

我知道这听起来像是 this 或其他一些的重复,但请耐心等待。

我有一个非常基本的 JAX-RS 资源,添加了我在遵循 here 的本教程中看到的所有必需注释。
但我在 Eclipse 的控制台中不断收到HTTP Status 500 和以下日志输出。

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;
    }
}

CustomResource.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。

【问题讨论】:

  • 你可以尝试使用javax.xml.bind.annotation.XmlRootElement 看看会发生什么。
  • @PaulSamsotha 这实际上是我首先尝试的。但是javax.xml.bind.annotation.XmlRootElement 已被弃用并丢弃了一段时间。绑定已移至jakarta.xml.bind.annotation.XmlRootElement
  • 一切看起来都不错。也许尝试更改 maven-compiler 以使用 Java 11。您说您使用的是 Java 11,但 Maven 配置显示您使用的是 Java 7。
  • @PaulSamsotha 我将 pom.xml 中构建配置中的 &lt;source&gt; 和 &lt;target&gt; 值从 1.7 更改为 11。不幸的是,没有变化。

标签: jersey jax-rs


【解决方案1】:

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

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>3.0.2</version>
</dependency>

它解决了问题。

【讨论】:

    【解决方案2】:

    我遵循相同的教程并遇到相同的问题,我通过添加以下依赖项来修复它:

    `       <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>
    ` 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多