我正在使用 Spring 框架开发 REST API。我设置了一个后置控制器:
@PostMapping(path="/v2/document", consumes= {"application/xml","application/json"}, produces={"application/xml","application/json"})
@ResponseBody
public ResponseEntity<Object> postDocument(HttpServletRequest request,@RequestBody Template template) throws Exception
它接受一个 Template 对象: 公开课模板{
@XmlElement(name = "Callback")
private String Callback;
@XmlElement(name = "OutputFormat")
private String OutputFormat;
@XmlElement(name = "Data")
private byte[] Data;
@XmlElement(name = "ConnectionString")
private String ConnectionString;
@XmlElement(name = "Format")
private String Format;
@XmlElementWrapper(name="Properties")
@XmlElement(name = "Property")
private Property[] Properties;
现在这是有效的请求正文:
<Template>
<connectionString>ConString</connectionString>
<outputFormat>pdf</outputFormat>
<datasources>
<datasource>
<connectionString>ConString</connectionString>
</datasource>
</datasources>
</Template>
外部标签是正确的 PascalCase 格式,但内部标签必须是驼峰命名法。这就是我想要的身体外观:
<Template>
<ConnectionString>ConString</ConnectionString>
<OutputFormat>pdf</OutputFormat>
<Datasources>
<Datasource>
<ConnectionString>ConString</ConnectionString>
</Datasource>
</Datasources>
</Template>
我所要做的就是添加:
@XmlRootElement(name="Template")
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Template{
现在它按照我想要的方式工作了。
现在已弃用,您必须使用 PropertyNamingStrategies 而不是 PropertyNamingStrategy。
@XmlRootElement(name="Template")
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class Template{