Json spring:输出是xml,但我想要JSON

时间:2016-03-10 13:08:12

标签: 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具有到弹簧前端控制器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>

2 个答案:

答案 0 :(得分:0)

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

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

<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使用请求参数。

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

答案 1 :(得分:0)

尝试设置响应内容类型:

response.setContentType("application/json");