XmlElement(name=“custom_name”)在与rest服务集成的Spring Boot 中不工作

mwkjh3gx  于 2022-12-13  发布在  Spring
关注(0)|答案(5)|浏览(119)

我几乎是新的休息服务世界,在这里我试图改变字段名称显示在输出xml。
不确定,我是不是按照正确的方法,任何帮助都是一件好事。

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

活动控制器.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>

预期输出
从活动ID更改为活动ID

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

cxfofazt1#

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

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

q7solyqu2#

非常奇怪。但是如果你使用@JsonProperty(“actvID”)而不是@XmlElement(name=“actvID”),它对JSON和XML都有效。

g6ll5ycj

g6ll5ycj3#

在setter而不是getter中设置xml元素名称。

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

avwztpqn4#

@Xml* 注解将与Java SE的JAXB一起使用。如果您具有jackson-dataformat-xml 2.* 依赖关系,请删除该依赖关系,并让JAXB使用@Xml* 注解处理Map。如果您希望使用jackson-dataformat-xml 2.,则必须使用@Json Jackson注解集,该注解集将同时支持XML和JSONMap。

nc1teljy

nc1teljy5#

到目前为止,这对我很有效:

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

更新:升级到Jackson2.8.8spring 4.3.25.RELEASE后,所有注解都按预期工作。

相关问题