Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 765
Prash Avatar asked Nov 29 '16 20:11

Prash


2 Answers

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>
like image 103
cheb1k4 Avatar answered Nov 15 '22 08:11

cheb1k4


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

like image 45
Yuvaraj Mani Avatar answered Nov 15 '22 09:11

Yuvaraj Mani