【问题标题】:How to support both json and html response with Spring Boot如何使用 Spring Boot 同时支持 json 和 html 响应
【发布时间】:2015-06-10 12:28:23
【问题描述】:

我想将数据发送到移动设备(使用 json)和网络(使用 html),并且我正在使用 Spring boot。我想获得与 facebook(graph.facebook.com and facebook.com) 和 twitter(api.twitter.com and twitter.com) 使用的相似的架构和功能。我也使用 Angular.js 作为 Web 框架。

【问题讨论】:

    标签: angularjs spring spring-boot restful-architecture


    【解决方案1】:

    没有什么可以阻止您制作两种不同的实现(json、html)并从同一个 URL 提供它。只需使用正确的Consumable Media TypesProducible Media Types 例如:

    @Controller
    @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
    @ResponseBody
    public Pet getPet(@PathVariable String petId, Model model) {
        // implementation omitted
    }
    

    【讨论】:

    • 但我必须为这两个权利使用单独的 url。那么如果我使用的是Spring Boot,我应该如何配置呢。
    • 我想设置单独的 html 和 json 响应的 url。假设 json 的 baseurl 是:api.myapp.com,html 数据的 baseurl 是 myapp.com,现在我想在 Spring Boot 中创建上述控制器。我应该如何为相同的设置单独的基本网址。
    • 然后简单地制作两个控制器(甚至两个应用程序)。并且由于您想使用不同的域名,请使用像nginx这样的反向代理。
    • nginx 是 Web 服务器,与 Spring Boot 无关。
    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 2017-12-05
    • 2012-04-15
    • 2014-10-03
    • 2022-06-20
    • 2023-02-24
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多