如何在 Swagger 生成的 Spring 服务器代码中从 POST RequestBody 检索 XML 字符串?
How to retrieve an XML string from a POST RequestBody in Swagger-generated Spring server code?
我想创建一个 REST API 方法来接收 POST 发送的 XML 字符串。我正在使用 Swagger Editor 自顶向下设计我的 REST API 并生成服务器存根代码。
POST 方法在我的 swagger.yaml
中看起来像这样:
/models:
post:
summary: Store a model.
description: Stores the XML content of the specified model.
consumes:
- application/xml;charset=UTF-8
parameters:
- name: model
in: body
description: The model XML you want to store
schema:
type: string
required: true
responses:
201:
description: Model stored successfully
headers:
location:
description: URL of the stored model.
type: string
我在 yaml 文件中也有这个全局设置:
produces:
- application/json
当我使用 Swagger Editor 的 Generate Server > Spring 菜单选项时,会为 POST 方法生成以下接口方法:
@ApiOperation(value = "Store a model.", notes = "Stores the XML content of the specified model.", response = Void.class, tags={ })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Model stored successfully", response = Void.class) })
@RequestMapping(value = "/models",
produces = { "application/json" },
consumes = { "application/xml;charset=UTF-8" },
method = RequestMethod.POST)
ResponseEntity<Void> modelsPost(
@ApiParam(value = "The model XML you want to store" ,required=true ) @RequestBody String model);
这是相应的存根实现:
public ResponseEntity<Void> modelsPost(
@ApiParam(value = "The model XML you want to store" ,required=true ) @RequestBody String model
) {
// do some magic!
return new ResponseEntity<Void>(HttpStatus.OK);
}
我使用 Postman post 一些虚拟 XML 到我的 运行 Spring 引导服务上的方法:
但是当我使用 log.debug("Model XML = " + model);
在实现方法中打印出 model
的值时,我在日志中得到这样的输出:
Model XML = ------WebKitFormBoundaryA3o70hOgLFoLLBoY
Content-Disposition: form-data; name="model"
<?xml version="1.0" encoding="utf-8"?><Hello></Hello>
------WebKitFormBoundaryA3o70hOgLFoLLBoY--
如何让 XML 本身变成 model
的值?在这个例子中我希望它是这样的:
<?xml version="1.0" encoding="utf-8"?><Hello></Hello>
请记住,我不能直接编辑 Java 方法签名,因为 Swagger 编辑器正在生成它们。如果我的 swagger 定义是错误的,我应该使用什么来代替 post 一个 XML 字符串?
实践中的 XML 可能很大,因此不能将其作为请求参数发送。我也不打算处理 XML,所以可以将其视为字符串。
在邮递员中,从表单数据切换到原始数据,并select相关的内容类型(application/xml)。
你必须这样做,因为 spring 期望你的对象在请求正文中(因为你的参数上有 @RequestBody
注释)
如果您只有最少的转换器并且想将 Json/XML 读取为字符串,您还需要一个转换器来将内容类型转换为字符串。 StringHttpMessageConverter 就是这样做的。
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="false">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<constructor-arg ref="jaxbUnMarshaller" />
<property name="supportedMediaTypes">
<list>
<value>application/xml</value>
<value>text/xml</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain</value>
<value>application/xml</value> <!-- extract xml as string -->
</list>
</property>
<property name="writeAcceptCharset" value="true" />
</bean>
我想创建一个 REST API 方法来接收 POST 发送的 XML 字符串。我正在使用 Swagger Editor 自顶向下设计我的 REST API 并生成服务器存根代码。
POST 方法在我的 swagger.yaml
中看起来像这样:
/models:
post:
summary: Store a model.
description: Stores the XML content of the specified model.
consumes:
- application/xml;charset=UTF-8
parameters:
- name: model
in: body
description: The model XML you want to store
schema:
type: string
required: true
responses:
201:
description: Model stored successfully
headers:
location:
description: URL of the stored model.
type: string
我在 yaml 文件中也有这个全局设置:
produces:
- application/json
当我使用 Swagger Editor 的 Generate Server > Spring 菜单选项时,会为 POST 方法生成以下接口方法:
@ApiOperation(value = "Store a model.", notes = "Stores the XML content of the specified model.", response = Void.class, tags={ })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Model stored successfully", response = Void.class) })
@RequestMapping(value = "/models",
produces = { "application/json" },
consumes = { "application/xml;charset=UTF-8" },
method = RequestMethod.POST)
ResponseEntity<Void> modelsPost(
@ApiParam(value = "The model XML you want to store" ,required=true ) @RequestBody String model);
这是相应的存根实现:
public ResponseEntity<Void> modelsPost(
@ApiParam(value = "The model XML you want to store" ,required=true ) @RequestBody String model
) {
// do some magic!
return new ResponseEntity<Void>(HttpStatus.OK);
}
我使用 Postman post 一些虚拟 XML 到我的 运行 Spring 引导服务上的方法:
但是当我使用 log.debug("Model XML = " + model);
在实现方法中打印出 model
的值时,我在日志中得到这样的输出:
Model XML = ------WebKitFormBoundaryA3o70hOgLFoLLBoY
Content-Disposition: form-data; name="model"
<?xml version="1.0" encoding="utf-8"?><Hello></Hello>
------WebKitFormBoundaryA3o70hOgLFoLLBoY--
如何让 XML 本身变成 model
的值?在这个例子中我希望它是这样的:
<?xml version="1.0" encoding="utf-8"?><Hello></Hello>
请记住,我不能直接编辑 Java 方法签名,因为 Swagger 编辑器正在生成它们。如果我的 swagger 定义是错误的,我应该使用什么来代替 post 一个 XML 字符串?
实践中的 XML 可能很大,因此不能将其作为请求参数发送。我也不打算处理 XML,所以可以将其视为字符串。
在邮递员中,从表单数据切换到原始数据,并select相关的内容类型(application/xml)。
你必须这样做,因为 spring 期望你的对象在请求正文中(因为你的参数上有 @RequestBody
注释)
如果您只有最少的转换器并且想将 Json/XML 读取为字符串,您还需要一个转换器来将内容类型转换为字符串。 StringHttpMessageConverter 就是这样做的。
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="false">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<constructor-arg ref="jaxbUnMarshaller" />
<property name="supportedMediaTypes">
<list>
<value>application/xml</value>
<value>text/xml</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain</value>
<value>application/xml</value> <!-- extract xml as string -->
</list>
</property>
<property name="writeAcceptCharset" value="true" />
</bean>