Spring 3:@ResponseBody - 如何控制响应以获取 XML 或 JSON?

发布于 2024-11-16 08:38:19 字数 1607 浏览 2 评论 0原文

我正在使用 Spring (3.0) 配置(没有 ContentNegotiatingViewResolver)。根据 文档 Spring 3 支持 JSON 和 XML。在如下处理程序方法上使用 @ResponseBody 会给出 JSON 响应。如何控制响应以获取 XML 或 JSON ?

@RequestMapping("/data")
public @ResponseBody User getUser() {
    return new User();
}

编辑

使用的Maven依赖项:

<!-- xml -->
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.1.8</version>
</dependency>

<!-- json --> 
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.8.1</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.1</version>
</dependency>

调试AnnotationDrivenBeanDefinitionParser =>; jaxb2Present = true,jacksonPresent = true

I'm using <mvc:annotation-driven /> Spring (3.0) configuration (without ContentNegotiatingViewResolver). According to the documentation Spring 3 supports JSON and XML. Using @ResponseBody on handler method like below gives JSON response. How to control the response to get the XML or JSON ?

@RequestMapping("/data")
public @ResponseBody User getUser() {
    return new User();
}

EDIT

Used Maven dependencies:

<!-- xml -->
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.1.8</version>
</dependency>

<!-- json --> 
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.8.1</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.1</version>
</dependency>

Debugging AnnotationDrivenBeanDefinitionParser => jaxb2Present = true, jacksonPresent = true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-11-23 08:38:19

Spring 使用请求的“Accept”标头来决定是发送 JSON 还是 XML。如果两者都被接受,那么你会得到一个或另一个,我不记得首先被检查。

要获取 XML,您的客户端需要在标头中包含“application/xml”,而不是“application/json”。

Spring uses the "Accept" header of the request to decide whether to send JSON or XML. If both are accepted, then you you'll get one or the other, I can't remember gets checked first.

To get an XML, your client needs to have "application/xml" in the header, rather than "application/json ".

演多会厌 2024-11-23 08:38:19

像这样使用@ResponseBody。下面的方法将 JSON 作为请求参数并返回 JSON 响应。

@RequestMapping(value = "...", method = RequestMethod.POST)
@ResponseBody
public Object RestPOSTService(@RequestBody JsonContent content,
        HttpServletRequest request) throws Exception {
        .....
    return (JSON);
}

Use @ResponseBody like this. Method below takes JSON as request parameter and returns JSON response.

@RequestMapping(value = "...", method = RequestMethod.POST)
@ResponseBody
public Object RestPOSTService(@RequestBody JsonContent content,
        HttpServletRequest request) throws Exception {
        .....
    return (JSON);
}
泡沫很甜 2024-11-23 08:38:19

它在RequestMapping中配置

@RequestMapping(
 value = "...", 
 method = {RequestMethod.POST},produces ="application/json")
 public @ResponseBody Object getObject() { ... } 

It's configured in RequestMapping

@RequestMapping(
 value = "...", 
 method = {RequestMethod.POST},produces ="application/json")
 public @ResponseBody Object getObject() { ... } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文