简体   繁体   English

如何使用/进行内容协商来提供静态内容和spring数据?

[英]How can I serve static content and spring data rest at / using content negotiation?

I would like to host both static content, and spring data rest on / 我想同时托管静态内容,并且spring数据位于/

curl http://localhost:8080                                                                                                                                       slave-vi
<!DOCTYPE html><html><head><title>RPF</title><meta name="viewport" content="width=device-width,initial-scale=1"><base href="/"><link href="styles.css" rel="stylesheet"></head><body aurelia-app="main"><div class="splash"><div class="message">RPF</div><i class="fa fa-spinner fa-spin"></i></div><script type="text/javascript" src="aurelia-bootstrap.3c6271fc099630981613.bundle.js"></script><script type="text/javascript" src="aurelia.042f8d07b45053bfe6a6.bundle.js"></script><script type="text/javascript" src="app.86af2df503886ba0a486.bundle.js"></script></body></html>

where the only difference should be based on on content negotiation 唯一的区别应基于内容协商

curl -H "Accept: application/json" http://localhost:8080                                                                                                    slave-vi
{
  "_links" : {
    "users" : {
      "href" : "http://localhost:8080/users{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile"
    }
  }
}

So if browser Accept headers (they're kind of non specific) or no accept headers are sent, I'd get the index.html but if application/json or other specific content headers are sent I'd get that type or a 415 depending on supportedness. 因此,如果浏览器的Accept标头(不是特定类型的)或没有发送Accept标头,我会得到index.html但是如果发送application/json或其他特定的内容标头,我会得到该类型或415取决于支持程度。

currently I set static locations by adding this on the command line -Dspring.resources.staticLocations=... 当前,我通过在命令行上添加静态位置来设置静态位置-Dspring.resources.staticLocations=...

You could have a Controller listen for all requests on / and forward them based on some condition: 您可以让Controller侦听/上的所有请求,并根据某些条件转发它们:

@RequestMapping("/")
public String handleIndexPage(@RequestHeader(value="Accept") String acceptValue) {
    if (MediaType.APPLICATION_JSON_VALUE.equals(acceptValue)) {
        return "forward:/spring-data-rest-url";
    } else {
        return "forward:/other-url";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM