首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么spring-data-rest中的alps (或配置文件)路径返回json body和不匹配的头文件Content-Type: text/html?

为什么spring-data-rest中的alps (或配置文件)路径返回json body和不匹配的头文件Content-Type: text/html?
EN

Stack Overflow用户
提问于 2017-02-03 09:20:48
回答 1查看 414关注 0票数 6

现在,我的spring-data-rest (spring-boot 1.4.3.RELEASE)提供的控制器的默认Content-Type是application/hal+json,这很有意义。例如,如果我使用chrome,我会得到应用程序根目录的application/hal+json,因为chrome使用的是Accept头文件"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"。然而,/profile (正式的/alps) URL提供了文本/html,即使响应主体是json (使得内容类型与主体不匹配)。如果您专门只请求application/json,那么您将获得正确的响应头。

以下是错误的工作情况(当返回的文档/正文不是text/html时,返回text/html):

代码语言:javascript
复制
$ http --verbose "http://localhost:8080/v1/profile/eldEvents" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"

GET /v1/profile/eldEvents HTTP/1.1
Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/0.9.2



HTTP/1.1 200 
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Location, X-Auth, Authorization
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Location
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: text/html;charset=UTF-8
Date: Fri, 03 Feb 2017 01:16:14 GMT
Expires: 0
Pragma: no-cache
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
Transfer-Encoding: chunked
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
  "alps" : {
    "version" : "1.0",
    "descriptors" : [ {
      "id" : "eldEvent-representation",
      "href" : "http://localhost:8080/v1/profile/eldEvents",
      "descriptors" : [ {
        "name" : "sequenceId",
        "type" : "SEMANTIC"
      }, {
...

去掉响应的其余部分,你可以从上面看到它是json数据。

我认为上述请求的正确Content-Type应该类似于"application/json“。

EN

回答 1

Stack Overflow用户

发布于 2020-06-05 04:09:44

如果这仍然与您相关:我已经通过手动覆盖所有没有定义content-type/profile/*请求解决了这个问题。

代码语言:javascript
复制
@Component
public class ProfileContentTypeFilter extends OncePerRequestFilter
{
    private static final AntPathMatcher matcher = new AntPathMatcher();

    @Override
    protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
    throws ServletException, IOException
    {
        if (request.getContentType() == null && matcher.match("/profile/*", request.getRequestURI()))
        {
            // Override response content type for unspecified requests on profile endpoints
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        }

        filterChain.doFilter(request, response);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42015237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档