Spring-boot 从控制器返回 json 和 xml [英] Spring-boot return json and xml from controllers

查看:20
本文介绍了Spring-boot 从控制器返回 json 和 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 spring-boot 1.1.7 应用程序,它在大部分 UI 中使用 Thymeleaf,所以我的控制器的响应并没有真正成为问题.但是,现在我需要在用户通过 URL 提交请求时提供 XML 响应.

I have a spring-boot 1.1.7 application that uses Thymeleaf for much of the UI, so the response from my controllers hasn't really been a concern. However, now I need to provide a XML response when a user submits a request via URL.

这是一个典型的请求:

http://localhost:9001/remote/search?sdnName=Victoria&address=123 Maple Ave

这是我的大部分 gradle 配置:

Here is most of my gradle configuration:

project.ext {
    springBootVersion = '1.1.7.RELEASE'
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
    compile("org.springframework.security:spring-security-web:4.0.0.M1")
    compile("org.springframework.security:spring-security-config:4.0.0.M1")
    compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.5.0')
}

这是我的控制器:

@Controller
public class RemoteSearchController {

    @Autowired
    private SdnSearchService sdnSearchService;

    @RequestMapping(value = "/remote/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
    public List<Sdn> search(@ModelAttribute SdnSearch sdnSearch) {
        List<Sdn> foundSdns = sdnSearchService.find( sdnSearch );
        return foundSdns;
}

这是我要返回的对象:

@Entity
public class Sdn {

    @Id
    private long entNum;
    private String sdnName;
...
//getters & setters here
}

我能够通过 REST 客户端(例如 CocoaREST)接收请求并处理它.但是当我返回 SDN 列表时,我得到以下异常,即使我有 Jackson &我的类路径上的 jackson-dataformat-xml:

I am able to receive the request via REST client (such as CocoaREST) and handle it. But When I return the list of SDN i get the following exception, even though I do have Jackson & jackson-dataformat-xml on my classpath:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:229)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:301)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:248)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:57)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:299)

我的 REST 客户端包含一个text/xml"的接受标头(但老实说,我宁愿他们不必设置它.理想情况下,对该控制器的任何调用都将始终获得 XML,无论标头是否存在).

My REST Client is including a Accept Header of "text/xml" (but in all honesty I would rather them not have to set this. Ideally any call to this Controller would always get XML, regardless of header being present).

有没有办法解决这个问题?我以为媒体转换器被包括在内,只是返回控制器告诉他们的任何内容?

Is there a way to handle this? I thought the Media Converters were included and just returned whatever the controller told them to?

解决方案:请参阅下面我发布的答案.

SOLUTION: See below for the answer I posted.

推荐答案

解决方案:我结合使用了以下两个答案(非常感谢!).我在这里发帖以防其他人需要帮助.

SOLUTION: I used a combination of both answers below (thank you very much!). I am posting here in case anyone else needs help.

我修改后的控制器:

@Controller
public class RemoteSearchController {

    @Autowired
    private SdnSearchService sdnSearchService;

    @RequestMapping(value = "/remote/search", method = RequestMethod.GET, produces = { "application/xml", "text/xml" }, consumes = MediaType.ALL_VALUE )
    @ResponseBody
    public SdnSearchResults search(@ModelAttribute SdnSearch sdnSearch) {
        List<Sdn> foundSdns = sdnSearchService.find( sdnSearch );
        SdnSearchResults results = new SdnSearchResults();
        results.setSdns( foundSdns );
        return results;
    }
}

在我的客户端上,我设置了请求标头:

And on my client, I set the request headers:

内容类型:应用程序/文本接受:文本/xml我认为最终的问题是我的客户端标头设置不正确,所以我可能不必进行其中一些更改.但我喜欢包含结果列表的 SearchResults 类的想法:

Content-type: application/text Accept: text/xml I think ultimately the problem was that my client headers were not being set correctly, so I may not have had to make some of these changes. But I liked the idea of a SearchResults class containing a list of results:

@XmlRootElement
public class SdnSearchResults {
    private List<Sdn> sdns;
...
}

这篇关于Spring-boot 从控制器返回 json 和 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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