Java Maven Jersey 代码在尝试渲染 xml 时给出错误 MessageBodyWriter not found for media type=application/xml

问题描述 投票:0回答:1

我有一个 Jersey 代码,其中包含用于处理请求的 AlienResource 文件和 Alien.java 代码以及一些依赖项。我正在尝试在网页上呈现 xml 内容,但它给出错误,因为找不到 application/xml。

AlienResource.java

package com.Ritesh.Jerseyyy;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("alien")
public class AlienResource {
    
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Alien getAlien() {
        Alien a = new Alien();
        a.setAid(101);
        a.setAname("Ritesh");
        return a;
    }
}

外星人.java

package com.Ritesh.Jerseyyy;

import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Alien {
    private int aid;
    private String aname;
    public int getAid() {
        return aid;
    }
    public void setAid(int aid) {
        this.aid = aid;
    }
    public String getAname() {
        return aname;
    }
    public void setAname(String aname) {
        this.aname = aname;
    }
    @Override
    public String toString() {
        return "Alien [aid=" + aid + ", aname=" + aname + "]";
    }

}

这个 pom.xml 文件具有以下依赖项

<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>
<!-- uncomment this to get JSON support-->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-binding</artifactId>
</dependency>

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>4.0.1</version>
</dependency>

我收到严重错误:未找到媒体类型 = application/xml 的 MessageBodyWriter

有人可以帮忙吗?

我收到错误并希望解决它

java jersey
1个回答
0
投票

错误:

未找到MessageBodyWriter

添加 jakarta.ws.rs-api

<dependency>
    <groupId>jakarta.ws.rs</groupId>
    <artifactId>jakarta.ws.rs-api</artifactId>
    <version>3.0.0</version>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.