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

3 篇文章 0 订阅
3 篇文章 0 订阅

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值