首页 > 解决方案 > 如何将 Swagger 的默认“响应内容类型”更改为“application/json”?

问题描述

SAP Commerce Cloud 版本:1905.9

目前,Swagger 的默认“响应内容类型”有 2 个值:“application/xml”和“application/json”。默认选择是“应用程序/xml”。有没有办法将默认选择更改为“application/json”?

示例香草代码是这样的:

@ApiOperation(hidden = true, value = "Updates the total amount of a specific product", notes = "Updates the total amount of a specific product in the cart based on the entryNumber.")
@RequestMappingOverride(priorityProperty = "b2bocc.CartResource.updateCartEntry.priority")
@RequestMapping(value = "/{cartId}/entries/{entryNumber}", method = RequestMethod.PUT)
@ResponseBody
@ApiBaseSiteIdUserIdAndCartIdParam
public CartModificationWsDTO updateCartEntry(@ApiParam(value = "Base site identifier.") @PathVariable final String baseSiteId,
        @ApiParam(value = "The id of the entry in the cart.") @PathVariable final int entryNumber,
        @ApiParam(value = "New quantity for this entry.") @RequestParam(required = true) final Long quantity,
        @ApiParam(value = "Response configuration. This is the list of fields that should be returned in the response body.", allowableValues = "BASIC, DEFAULT, FULL") @RequestParam(required = false, defaultValue = FieldSetLevelHelper.DEFAULT_LEVEL) final String fields)
{

    return updateCartEntry(baseSiteId, null, entryNumber, quantity, fields);
}

标签: swaggerhybris

解决方案


您可以在springmvc-servlet.xml中切换转换器的顺序

    <mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="jsonHttpMessageConverter"/>
            <ref bean="xmlHttpMessageConverter"/>
<!--            <ref bean="customJsonHttpMessageConverter" /> -->
<!--            <ref bean="customXmlHttpMessageConverter" /> -->
        </mvc:message-converters>
    </mvc:annotation-driven>    

推荐阅读