2016-09-19 79 views
3

目前我有一个Spring REST服务,它能够生成JSON和XML,但XML响应并不完全在我所需要的范围内。在Spring REST中自定义Jackson XML响应

这就是我建立至今:

的XSD对象

<xs:element name="project"> 
    <xs:complexType> 
     <xs:complexContent> 
      <xs:extension base="projectBase"/> 
     </xs:complexContent> 
    </xs:complexType> 
</xs:element> 

<xs:complexType name="projectBase"> 
    <xs:attribute type="xs:string" name="name" use="required" /> 
    <xs:attribute type="xs:string" name="id" use="optional" /> 
    <xs:attribute type="xs:anyURI" name="self" use="optional" /> 
</xs:complexType> 

的POM

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.dataformat</groupId> 
    <artifactId>jackson-dataformat-xml</artifactId> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-annotations</artifactId> 
</dependency> 

的控制器

@ApiOperation(value = "GET", 
     notes = "Returns a list of exisiting projects.") 
@RequestMapping(method = RequestMethod.GET, produces = {"application/smartask.projectsV1+xml", "application/smartask.projectsV1+json"}) 
public List<be.smartask.api.model.smartask.Project> getProjects(@ApiParam(value = "page of found projects", required = false) @RequestParam(value = "page", required = false, defaultValue = "0") int page, 
                   @ApiParam(value = "size(min:5, max:50) of projects on a page", required = false) @RequestParam(value = "size", required = false, defaultValue = "10") int size) throws ServiceException, ConversionException { 
    String uri = getURLWithContextPath(this.request); 
    List<be.smartask.api.model.smartask.Project> projects = projectConverter.convertToObjectList(projectService.findAll(page, size), uri); 
    return projects; 
} 

结果我当我执行这个g时得到etProjects的方法是

<List xmlns=""> 
    <item> 
    <name>project1</name> 
    <id>7a666889-9b03-4790-8100-d41a9de66a63</id> 
    <self>http://localhost/ask/projects/7a666889-9b03-4790-8100-d41a9de66a63</self> 
    </item> 
    <item> 
    <name>project2</name> 
    <id>f784ede1-d019-46e9-b7a4-83c51d6f7d8e</id> 
    <self>http://localhost/ask/projects/f784ede1-d019-46e9-b7a4-83c51d6f7d8e</self> 
    </item> 
    <item> 
    <name>project3</name> 
    <id>936e3ed9-b8c6-46ab-9570-60dcfcaaa670</id> 
    <self>http://localhost/ask/projects/936e3ed9-b8c6-46ab-9570-60dcfcaaa670</self> 
    </item> 
    <item> 
    <name>project4</name> 
    <id>4eaa6045-bbc4-423d-8bae-1da11692114b</id> 
    <self>http://localhost/ask/projects/4eaa6045-bbc4-423d-8bae-1da11692114b</self> 
    </item> 
</List> 

这已经很接近了,但我想用项目和项目替换List和Item标签。

到目前为止,我已经尝试添加各种注释像

  • @JacksonXmlElementWrapper(useWrapping = FALSE)
  • @JsonTypeInfo(使用= JsonTypeInfo.Id.NAME,包括= JsonTypeInfo.As.WRAPPER_OBJECT)
  • @JacksonXmlElementWrapper(useWrapping =真,的localName = “项目”)
  • @JacksonXmlProperty(=的localName “项目”)
  • @JacksonXmlRootElement(=的localName “项目”)

我在控制器方法上使用的第一个4和我在生成的对象(Project和ProjectBase)上使用的最后一个,但没有任何反应。

我在做什么错,或者我忘了做什么?

我想保持我的JSON输出,因为它虽然

[ 
    { 
    "name": "project1", 
    "id": "7a666889-9b03-4790-8100-d41a9de66a63", 
    "self": "http://localhost/ask/projects/7a666889-9b03-4790-8100-d41a9de66a63" 
    } 
] 

,而不是

{ 
    "Projects": { 
     "Project": [{ 
      "name": "project1", 
      "id": "7a666889-9b03-4790-8100-d41a9de66a63", 
      "self": "http://localhost/ask/projects/7a666889-9b03-4790-8100-d41a9de66a63" 
     }] 
    } 
} 

编辑#1

有人建议我去看看这个指南:

https://pascaldimassimo.com/2010/04/13/how-to-return-a-single-json-list-out-of-mappingjacksonjsonview/

但尚未成功进行此项工作。

回答

0

该解决方案没有尝试调整XML输出,而是调整JSON输出。我确实必须创建一个包装对象“Projects”。

<xs:element name="projects"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="project" minOccurs="0" maxOccurs="unbounded"> 
       <xsd:annotation> 
        <xsd:appinfo> 
        <annox:annotate target="getter">@com.fasterxml.jackson.annotation.JsonValue</annox:annotate> 
        </xsd:appinfo> 
       </xsd:annotation> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

导致

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "project" 
}) 
@XmlRootElement(name = "projects") 
public class Projects { 

    protected List<Project> project; 

    @JsonValue 
    public List<Project> getProject() { 
    if (project == null) { 
     project = new ArrayList<Project>(); 
    } 
    return this.project; 
    } 

} 

通过注释与@JsonValue,吸气,将拆除包装“项目”对象的JSON输出

你确实需要产生一些注释额外的设置尽管如此,参见:https://github.com/highsource/jaxb2-annotate-plugin