XmlElement(name=“custom_name”) not working in spring boot integrated with rest services

半城伤御伤魂 提交于 2020-12-03 07:12:22

问题


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();
        }
    }

Current Output

<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>

Expected output

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>

回答1:


Very strange. But if you use @JsonProperty("actvID") instead of @XmlElement(name="actvID") it works for both JSON as well as XML.




回答2:


This worked for me so far:

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 + "]";
}
}

UPDATE: after upgrading to jackson 2.8.8 and spring 4.3.25.RELEASE all the annotations worked as supposed.




回答3:


I know it's an old question but I encountered the same problem and solved it by registering a JaxbAnnotationModule in the object mapper of 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;
    }
}

I also had to add the maven dependency:

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


来源:https://stackoverflow.com/questions/40874820/xmlelementname-custom-name-not-working-in-spring-boot-integrated-with-rest-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!