为什么spring-data-rest中的alps(或profile)路径返回带有非匹配头的json主体Content-Type:text/html?

edg*_*dge 6 spring-data-rest

现在我的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提供text/html,即使响应主体是json(使得Content-Type与主体不匹配).如果您特别要求application/json,那么您将获得正确的响应标头.

这是错误工作的情况(当文档/正文返回时不返回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"
      }, {
...
Run Code Online (Sandbox Code Playgroud)

剪掉其余的响应,你可以从上面看到它是json数据.

我相信上述请求的正确Content-Type应该类似于"application/json".

Seb*_*ich 1

如果这仍然与您相关:我已经通过手动覆盖所有未定义的请求来解决这个/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);
    }
}
Run Code Online (Sandbox Code Playgroud)