Spring Boot 控制器建议 - 如何返回 XML 而不是 JSON?
Posted
技术标签:
【中文标题】Spring Boot 控制器建议 - 如何返回 XML 而不是 JSON?【英文标题】:Spring Boot Controller Advice - How to return XML instead of JSON? 【发布时间】:2020-01-12 12:44:49 【问题描述】:我有一个控制器建议类,但我似乎无法让它返回 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;
注意@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;
下面是一个抛出错误的示例休息控制器方法
@RequestMapping(value = "/error_test", produces = MediaType.APPLICATION_XML_VALUE)
public PriceAvailabilityResponse getPriceResponse() throws Exception
int x = 1/0;
return null;
我已经为此代码编写了模型,以根据哪个端点访问微服务来返回 JSON 和 XML,这是绝对必要的。
不幸的是,当我点击/error_test
路径时,我的响应总是以 JSON 格式。
如何强制响应为 XML?非常感谢您的宝贵时间。
【问题讨论】:
在你的邮递员中将 json 转换为 xml 我的邮递员没有更改回复。如果没有出现代码错误,我可以获得 XML 响应。如果我将该下拉框更改为“xml”,它只会重新格式化 json,而不是以下方法应该可以解决您的问题
@RestController
public class TestController
@GetMapping(value = "/throw-exception", produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity throwException()
throw new CustomException("My Exception");
从异常处理程序返回响应实体并用它指定媒体类型。
@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);
如果没有,请包含 jackson-dataformat-xml 依赖项
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
【讨论】:
非常感谢您的回答。我要到明天才能测试它,但请放心,我明天会测试它。非常感谢,如果它有效,我会告诉你。 嘿,我试图实现你的答案,我不知道在你的第二个代码块中用什么代替ExceptionDto
。我敢肯定这对大多数人来说是显而易见的,但我对弹簧靴很陌生。再次感谢。
@geekTechnique 我已经更新了答案。基本上 ExceptionDto 是我想从错误处理程序中抛出的响应体。您可以在错误中返回 priceAvailabilityResponse 作为响应正文。
这对我来说是个大问题。太感谢了;我非常感谢!这行得通,但我必须从你的中间代码块中删除 .build())
才能编译它。以上是关于Spring Boot 控制器建议 - 如何返回 XML 而不是 JSON?的主要内容,如果未能解决你的问题,请参考以下文章
如何在spring boot中从restful控制器返回一个html页面?
Spring Boot @ControllerAdvice 异常处理程序未触发