Jersey 与springMVC集成无法输出XML格式 报 MessageBodyWriter not found for media type=application/xml的解决办法

版权声明:如非本人授权,请勿转载 https://blog.csdn.net/gianttj/article/details/86168917

Jersey 与springMVC集成无法输出XML格式 报 MessageBodyWriter not found for media type=application/xml的解决办法,

在国内搜索引擎中搜到的都是type=application/xml关于type=application/json生成错误的,后来用bing国际版找了一篇外国大神的文章,总算解决问题了。

首先看看maven pom.xml文件中添加的依赖:

<!--jersey -->
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>2.25.1</version>
		</dependency>

		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet-core</artifactId>
			<version>2.25.1</version>
		</dependency>

		<dependency>
			<groupId>org.glassfish.jersey.media</groupId>
			<artifactId>jersey-media-multipart</artifactId>
			<version>2.25.1</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.media</groupId>
			<artifactId>jersey-media-json-jackson</artifactId>
			<version>2.25.1</version>
		</dependency>

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

然后是对象class定义里要加@XmlRootElement和每个get方法上面加@XmlElement,如下所示:

package com.duotun.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {
    private Integer id;

    private Integer groupId;

    private Integer groupId2;

    private Date dateAdded;

    private String title;

    private String content;
    @XmlElement
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    @XmlElement
    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }
    @XmlElement
    public Integer getGroupId2() {
        return groupId2;
    }

    public void setGroupId2(Integer groupId2) {
        this.groupId2 = groupId2;
    }
    @XmlElement
    public Date getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }
    @XmlElement
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    @XmlElement
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

这样问题解决。

国外大神的文章:REST Service Producing JSON & XML Response Bug and Fix in JDeveloper 12.2.1

猜你喜欢

转载自blog.csdn.net/gianttj/article/details/86168917