Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用@RestController获取XML响应_Java_Spring_Spring Boot - Fatal编程技术网

Java 无法使用@RestController获取XML响应

Java 无法使用@RestController获取XML响应,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个小示例,其中我使用@RestController,因为我们知道@RestController是@Controller和@ResponseBody的组合。 在下面的代码片段中,我可以得到json格式的响应,但是如果我想要XML格式的响应,我应该尝试什么? @RestController public class CreditCardController { @Autowired CreditCradService creditcardService

我有一个小示例,其中我使用@RestController,因为我们知道@RestController是@Controller和@ResponseBody的组合。 在下面的代码片段中,我可以得到json格式的响应,但是如果我想要XML格式的响应,我应该尝试什么?

     @RestController
     public class CreditCardController {

     @Autowired
     CreditCradService creditcardService;

     @RequestMapping(path = "/getAllCards")
     public List<CreditCard> credit() {
         return creditcardService.getAllCards();
    }
}

要成功返回XML负载,项目中需要以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
最后,您可以指定
@GetMapping
products
属性,就像您已经在做的那样:

@GetMapping(value = "/getAllCards", produces = MediaType.APPLICATION_XML_VALUE)
public List<CreditCard> credit(){
  return creditcartService.getAllCards();
}
@GetMapping(value=“/getAllCards”,products=MediaType.APPLICATION\u XML\u value)
公开名单信用证(){
返回creditcartService.getAllCards();
}

Hi@Rieckpil,感谢您宝贵的时间。你的建议很有效。非常感谢:)不客气,你能接受正确的答案吗,这样将来的开发者会很容易识别它吗?
@XmlRootElement
public class CreditCard {
   // your data class
}
@GetMapping(value = "/getAllCards", produces = MediaType.APPLICATION_XML_VALUE)
public List<CreditCard> credit(){
  return creditcartService.getAllCards();
}