【问题标题】:Spring 4 mvc REST XML and JSON responseSpring 4 mvc REST XML 和 JSON 响应
【发布时间】:2016-11-13 04:26:12
【问题描述】:

我试图在我的控制器中使用相同的方法生成 XML 和 JSON 响应。我用谷歌搜索,发现 JSON 是 spring 的默认值,因为 jackson 在类路径上。但是对于 XML,我必须添加 JAXB 依赖项,然后使用 JAXB 注释来注释我的模型。另外,我必须更改 @RequestMapping 注释的生产属性。

现在,我的默认行为已经改变,它返回一个 XML 响应。 我想在我的请求中添加 content-Type 标头并使用值 application/json 后,我的响应将是 JSON,但遗憾的是事实并非如此。 以下是我的代码:-

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'
    }
}

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

谢谢,

阿马尔

【问题讨论】:

  • JAXB 是 JDK 的一部分,您不需要添加该依赖项。返回的内容取决于您作为接受标头发送的内容(以及以何种顺序)以及您正在调用的 URL(.json 或 .xml 等扩展名也会影响返回的内容)。我希望杰克逊理解 JAXB 注释,您需要为此包含正确的 JAXB 依赖项。
  • 非常感谢,由于我无法接受评论作为答案,因此我接受了以下评论。

标签: json spring rest spring-mvc spring-boot


【解决方案1】:

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

【讨论】:

  • 这行得通,谢谢,我认为将内容类型设置为请求标头可以解决问题,但它是接受标头。
【解决方案2】:

为了以 Json 或 XML 发送响应,一种解决方案可能是使用内容协商策略。您可以使用 /person.json 或 /person.xml 等 URL。这些将产生 json 或 xml 响应。但首先,您必须配置主 Spring 引导类:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override

      public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false).
                favorParameter(true).
                parameterName("mediaType").
                ignoreAcceptHeader(true).
                useJaf(false).
                defaultContentType(MediaType.APPLICATION_JSON).
                mediaType("xml", MediaType.APPLICATION_XML).
                mediaType("json", MediaType.APPLICATION_JSON);
      }
    }

无需在控制器中添加produce = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE}Here is an example 希望这会有所帮助

【讨论】:

  • 感谢您提供此信息,我也会尝试一下
  • 太棒了。很高兴为您提供帮助
猜你喜欢
  • 2011-09-21
  • 1970-01-01
  • 2016-05-18
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
相关资源
最近更新 更多