本文介绍了XmlElement(name ="custom_name")在与其余服务集成的Spring Boot中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am almost new to rest services world,here i am trying to change the field name displayed in the output xml.

不确定,如果我遵循正确的方法,任何帮助都是一件好事.

Not sure,am i following the right method,any help is a good thing.

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

Changes from activityId to 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>

推荐答案

@ Xml *注释将与Java SE的JAXB一起使用.如果您具有jackson-dataformat-xml 2. *依赖项,请删除该依赖项,并让JAXB使用@ Xml *批注处理映射.如果要使用jackson-dataformat-xml 2. *,则必须使用@ Json *的Jackson注释集,它将支持XML和JSON映射.

@Xml* annotations will work with Java SE's JAXB. If you have jackson-dataformat-xml 2.* dependency remove that and let JAXB handles mapping using @Xml* annotations.If you want to use jackson-dataformat-xml 2.* then you have to use @Json* set of Jackson annotations which will support both XML and JSON mappings.

这篇关于XmlElement(name ="custom_name")在与其余服务集成的Spring Boot中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:42