Spring 4 MVC REST XML和JSON响应 [英] Spring 4 mvc REST XML and JSON response

查看:129
本文介绍了Spring 4 MVC REST XML和JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过控制器中的相同方法生成XML和JSON响应.我在Google上搜索,发现JSON是spring的默认值,因为jackson在classpath上.但是对于XML,我必须添加JAXB依赖关系,然后使用JAXB注释对模型进行注释.另外,我不得不更改@RequestMapping批注的Produces属性.

I am trying to produce XML and JSON response from the same method in my controller. I googled and found that JSON is the default for spring, as the jackson is on classpath. But for XML, I had to add the JAXB dependency and then annotate my model with JAXB annotation. Also, I had to change the produces attribute of the @RequestMapping annotation.

现在,我的默认行为已更改,它返回一个XML响应. 我以为在将content-Type标头添加到带有值application/json的请求中之后,我的响应将是JSON,但遗憾的是事实并非如此. 下面是我的代码:-

Now, my default behavior has changed, it returns an XML response. I thought after adding the content-Type header to my request with the value application/json, my response will be JSON, but sadly this is not the case. Below is my code:-

package com.example.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Person;

@RestController("person")
public class PersonController {

    @RequestMapping(name ="getperson", produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
    public Person getPerson() {
        Person p = new Person();
        p.setAge(28);
        p.setEmail("email@gmail.com");
        p.setName("Amar");
        return p;
    }

}

模型类:-

package com.example.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

    //@XmlElement
    private String name;
    //@XmlElement
    private int age;
    private String email;

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    public String getEmail() {
        return email;
    }
    @XmlElement
    public void setEmail(String email) {
        this.email = email;
    }
}

构建文件:-

buildscript {
    ext {
        springBootVersion = '1.3.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot' 

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('javax.xml.bind:jaxb-api:2.2.12')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

我做错了什么??? 非常感谢这里的任何帮助.

What am I doing wrong ??? Any help here is much appreciated.

谢谢

阿玛尔

推荐答案

为了从服务器获取json响应,您的请求必须包含设置为application/jsonAccept标头.

In order to get json response from the server your request must contain Accept header set to application/json.

这篇关于Spring 4 MVC REST XML和JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆