gpt4 book ai didi

rest - XmlElement(name ="custom_name") 在与休息服务集成的 Spring Boot 中不起作用

转载 作者:行者123 更新时间:2023-12-03 14:44:49 25 4
gpt4 key购买 nike

我几乎是休息服务世界的新手,在这里我试图更改输出 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>

预期输出

从事件 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>

最佳答案

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

关于rest - XmlElement(name ="custom_name") 在与休息服务集成的 Spring Boot 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40874820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com