XmlElement(name =" custom_name")在与spring服务集成的spring boot中不起作用

时间:2016-11-29 20:04:02

标签: rest spring-boot jaxb spring-restcontroller spring-rest

我几乎是休息服务世界的新手,我在这里尝试更改输出xml中显示的字段名称。

不确定,我是否遵循正确的方法,任何帮助都是件好事。

Activity.java

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Activity {

    private int activityId;
    private int duration;
    private String  description;


    public Activity(int activityId, int duration, String description) {
        super();
        this.activityId = activityId;
        this.duration = duration;
        this.description = description;
    }

    @XmlElement(name="actvID")
    public int getActivityId() {
        return activityId;
    }
    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "Activity [activityId=" + activityId + ", duration=" + duration + ", description=" + description + "]";
    }
}

ActivityController.java

 import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    import com.rest.domains.Activity;
    import com.rest.services.ActivityService;

    @Controller
    @RequestMapping("/activity")
    public class ActivityController {

        private ActivityService activityService;

        @Autowired
        public void setActivityService(ActivityService activityService) {
            this.activityService = activityService;
        }

        @RequestMapping(value="/list",method=RequestMethod.GET,produces=MediaType.APPLICATION_XML_VALUE)
        @ResponseBody
        public List<Activity> getAllActivities(){
            return activityService.getAllActivities();
        }
    }

当前输出

<List>
<item>
<activityId>1</activityId>
<duration>10</duration>
<description>walking</description>
</item>
<item>
<activityId>2</activityId>
<duration>20</duration>
<description>swimming</description>
</item>
<item>
<activityId>3</activityId>
<duration>30</duration>
<description>gym</description>
</item>
</List>

预期输出

从activityId更改为actvID

<List>
<item>
<actvID>1</actvID>
<duration>10</duration>
<description>walking</description>
</item>
<item>
<actvID>2</actvID>
<duration>20</duration>
<description>swimming</description>
</item>
<item>
<actvID>3</actvID>
<duration>30</duration>
<description>gym</description>
</item>
</List>

4 个答案:

答案 0 :(得分:0)

很奇怪。 但是,如果您使用@JsonProperty(“ actvID”)而不是@XmlElement(name =“ actvID”),则它适用于JSON和XML。

答案 1 :(得分:0)

到目前为止,这对我有用:

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

@XmlRootElement
@XmlType(propOrder={"actvID", "duration", "description"})
public class Activity {

private int activityId;
private int duration;
private String  description;


public Activity(int activityId, int duration, String description) {
    super();
    this.activityId = activityId;
    this.duration = duration;
    this.description = description;
}



// todo - fix this abomination
public String getActvID() { return getActivityId(); }
public void setActvID(String activityId) { setActivityId(activityId); }
// todo - fix this abomination

public int getActivityId() { return activityId; }
public void setActivityId(int activityId) { this.activityId = activityId; }



public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
@Override
public String toString() {
    return "Activity [activityId=" + activityId + ", duration=" + duration + ", description=" + description + "]";
}
}

更新: 升级到 jackson 2.8.8 spring 4.3.25.RELEASE 后,所有注释均按预期工作。

答案 2 :(得分:0)

我知道这是一个老问题,但是我遇到了同样的问题,并通过在JaxbAnnotationModule的对象映射器中注册MappingJackson2XmlHttpMessageConverter来解决了这个问题:

import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;

@Configuration
public class XmlConfiguration {

    @Bean
    MappingJackson2XmlHttpMessageConverter getMappingJackson2XmlHttpMessageConverter() {
        var jaxbAnnotationModule = new JaxbAnnotationModule();
        var mappingJackson2XmlHttpMessageConverter = new MappingJackson2XmlHttpMessageConverter();
        mappingJackson2XmlHttpMessageConverter.getObjectMapper().registerModule(jaxbAnnotationModule);
        return mappingJackson2XmlHttpMessageConverter;
    }
}

我还必须添加maven依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.11.3</version>
</dependency>

答案 3 :(得分:0)

这将起作用。在 setter 而不是 getter 中设置 xml 元素名称

@XmlElement(name="Test")
public void setTest