Spring Boot 控制器建议 - 如何返回 XML 而不是 JSON?

gee*_*que 5 xml spring json spring-boot controller-advice

我有一个控制器建议类,但我似乎无法让它返回 XML,即使我使用了注释@RequestMapping。这是一个精简的示例。

@RestControllerAdvice
public class ControllerAdvice {    
    @ExceptionHandler(Exception.class)
    @RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
    public PriceAvailabilityResponse handleControllerErrorXML(final Exception e) {
        e.printStackTrace();
        System.out.println("Exception Handler functional");
    
        PriceAvailabilityResponse priceAvailabilityResponse = new PriceAvailabilityResponse();
        priceAvailabilityResponse.setStatusMessage("Server Error");
        priceAvailabilityResponse.setStatusCode(99);
        
        return priceAvailabilityResponse;
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意其余控制器如何@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)工作来控制响应的形成方式。

PriceAvailabilityResponse这是上述代码块中可能包含的内容的示例。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Getter
@Setter
public class PriceAvailabilityResponse {
    @JacksonXmlProperty(isAttribute = true, localName = "StatusCode")
    @JsonProperty(value = "StatusCode", required = false)
    private int statusCode = 0;

    @JacksonXmlProperty(isAttribute = true, localName = "StatusMessage")
    @JsonProperty(value = "StatusMessage", required = false)
    private String statusMessage;
}
Run Code Online (Sandbox Code Playgroud)

下面是抛出错误的示例休息控制器方法

@RequestMapping(value = "/error_test", produces = MediaType.APPLICATION_XML_VALUE)
public PriceAvailabilityResponse getPriceResponse() throws Exception{
    int x = 1/0;
    return null;
}
Run Code Online (Sandbox Code Playgroud)

我已经为此代码编写了模型,以返回 JSON 和 XML,具体取决于哪个端点正在访问微服务,这是绝对必要的。

不幸的是,当我踏上这/error_test条路时,我的响应总是以 JSON 形式出现。

在此输入图像描述

我如何强制响应为 XML?非常感谢您抽出时间。

Sur*_*raj 12

以下方法应该可以解决您的问题

@RestController
public class TestController {

    @GetMapping(value = "/throw-exception", produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity throwException(){
        throw new CustomException("My Exception");
    }
}
Run Code Online (Sandbox Code Playgroud)

从异常处理程序返回响应实体并用它指定媒体类型。

@ControllerAdvice
public class GlobalErrorHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = {CustomException.class})
protected ResponseEntity handleInvalidDataException(
        RuntimeException ex, WebRequest request) {

    PriceAvailabilityResponse priceAvailabilityResponse = new 
    PriceAvailabilityResponse();
    priceAvailabilityResponse.setStatusMessage("Server Error");
    priceAvailabilityResponse.setStatusCode(99);

    return ResponseEntity.status(HttpStatus.BAD_REQUEST)
            .contentType(MediaType.APPLICATION_XML)
            .body(priceAvailabilityResponse);
}
Run Code Online (Sandbox Code Playgroud)

如果没有,请包含 jackson-dataformat-xml 依赖项

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.9.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)