我对休息服务世界几乎是新手,在这里我尝试更改输出 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>
预期产量
从activityId更改为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>
我知道这是一个老问题,但我遇到了同样的问题,并通过在
JaxbAnnotationModule
的对象映射器中注册 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;
}
}
我还必须添加 Maven 依赖项:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.3</version>
</dependency>
@Xml* 注释将与 Java SE 的 JAXB 配合使用。如果您有 jackson-dataformat-xml 2.* 依赖项,请删除该依赖项并让 JAXB 使用 @Xml* 注释处理映射。 如果您想使用 jackson-dataformat-xml 2.* 那么您必须使用 @Json* 的 Jackson 注释集,它将支持 XML 和 JSON 映射。
很奇怪。 但是,如果您使用 @JsonProperty("actvID") 而不是 @XmlElement(name="actvID"),它适用于 JSON 和 XML。
这将起作用。在setter而不是getter中设置xml元素名称
@XmlElement(name="Test")
public void setTest
到目前为止这对我有用:
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 + "]";
}
}
更新: 升级到 jackson 2.8.8 和 spring 4.3.25.RELEASE 后,所有注释都按预期工作。
使用 Jackson 注释代替 javax/jakarta 注释。
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "customClassName")
public MyClass {
@JacksonXmlProperty(localName = "customFieldName")
private String myField;
....
}