gpt4 book ai didi

spring-data-rest - 为什么 spring-data-rest 中的 alps(或配置文件)路径返回带有不匹配 header Content-Type : text/html? 的 json 正文

转载 作者:行者123 更新时间:2023-12-03 14:50:17 25 4
gpt4 key购买 nike

现在,我的 spring-data-rest ( spring-boot 1.4.3.RELEASE ) 提供的 Controller 的默认 Content-Type 是 application/hal+json这是有道理的。如果我使用 chrome,我会得到 application/hal+json例如,对于我的应用程序的根目录,因为 chrome 使用了 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" 的 Accept header .然而,/profile (正式 /alps )即使响应正文是 json(使 Content-Type 与正文不匹配),URL 也提供 text/html。如果您只要求 application/json,那么您将获得正确的响应 header 。

这是工作不正确的情况(当返回的文档/正文不是 text/html 时,返回 text/html):

$ 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”。

最佳答案

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

@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);
}
}

关于spring-data-rest - 为什么 spring-data-rest 中的 alps(或配置文件)路径返回带有不匹配 header Content-Type : text/html? 的 json 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42015237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com