java - Json Spring : output is xml but I want JSON

标签 java json xml spring spring-mvc

所有代码都工作正常,但使用代码生成的输出是 XML 而不是 json,有人可以帮助我如何从此代码获取 JSON 格式输出。

这是 Controller 类

package spring;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@RestController

public class Main {

@RequestMapping(value="/main",method = RequestMethod.GET,headers =    {"Accept=text/xml, application/json"},produces = "application/json")
@ResponseBody()
public  ResponseEntity<Student> f()
    {

        Student s=new Student();
        s.setName("Nikesh Joshi");
        s.setAge(21);
        s.setId(1);
        return new ResponseEntity<Student>(s, HttpStatus.OK);
    }
    }

这是模型类,

package spring;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement; // I dont know which one to use

import com.sun.xml.internal.txw2.annotation.XmlElement; //this or above

@XmlRootElement
public class Student implements Serializable {
private static final long serialVersionUID = 2646831820313826686L;
private String name;
private int id;
private int age;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
}

web.xml 有到 spring 前端 Controller Dispatchers servlet 的简单映射。
输出是这样的 XML 格式 Output

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- Detects annotations like @Component, @Service, @Controller,    @Repository, @Configuration -->
   <context:component-scan base-package="spring"/>

   <!-- Detects MVC annotations like @RequestMapping -->
   <mvc:annotation-driven/>
   </beans>

最佳答案

使用什么类型的客户端来请求您的服务。如果您使用浏览器,它会优先使用 xml 而不是 json(接受:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 for firefox)。 Spring 使用 Accept header 进行内容协商。您的服务同时启用了 xml 和 json,因为 JAXB 和 JSON 都位于类路径上。

最好的方法是使用请求参数或文件扩展名来影响内容协商,而不是依赖于所有客户端都难以控制的 Accept header 。为此,请将以下内容添加到您的应用程序上下文中

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="true" />
<property name="mediaTypes" >
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>

要访问您的 xml 服务,您可以使用例如 http://host:port/context/resource.xml使用扩展名或 http://host:port/context/resource?format=xml使用请求参数

要访问您的 json 服务,您可以使用例如 http://host:port/context/resource.json使用扩展名或 http://host:port/context/resource?format=json使用请求参数。

注意。 @RequestMapping 的 Produces 属性不用于内容协商,而是用于缩小处理程序方法

关于java - Json Spring : output is xml but I want JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917442/

相关文章:

java - 如何使用 `Collections.binarySearch()` 通过对象的 ArrayList 进行二分搜索?

java - 使用 KeyEvent 时遇到问题

java - 你如何处理 Java 中的 "impossible"异常?

java - 在 Java 中扩展 JFrame 的类上绘画

xml - Inno Setup 根据自定义输入修改 XML 文件

php - magento 1.9.1.0 local.xml 不工作

xml - ASP.NET 网络 API : XMLFormatter thinks he can write Json-objects

json - 如何在 AWS CLI SNS 消息中提供 JSON?

android - Android 中的 Facebook json 解析

json - 有什么方便的方法可以在没有类型断言的情况下获取 JSON 元素吗?