首页 > 解决方案 > 如何解决 Spring 3.0 MVC 中的“mediaType”不能为空错误?

问题描述

如何解决mediaTypeSpring 3.0 MVC 中不能为空的错误?我们希望为移动应用程序实现“支持通用链接”的要求之一:

文件路径应该是:http://localhost:9080/.well-known/apple-app-site-association当我请求 URL 时,https://localhost:9080/.well-known/apple-app-site-association我们收到以下错误:

错误:

[5/7/18 10:54:38:776 EDT] 00000125 SystemOut     O 2018-05-07 10:54:38,757 ERROR PageController - Exception while processing request for: /.well-known/apple-app-site-association
java.lang.IllegalArgumentException: 'mediaType' must not be empty
    at org.springframework.util.Assert.hasLength(Assert.java:136) ~[spring-core-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:687) ~[spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at com.sdl.webapp.common.impl.interceptor.StaticContentInterceptor.fallbackForContentProvider(StaticContentInterceptor.java:79) ~[dxa-common-impl-1.5.0.jar:1.5.0]
    at com.sdl.webapp.common.impl.interceptor.StaticContentInterceptor.preHandle(StaticContentInterceptor.java:111) ~[dxa-common-impl-1.5.0.jar:1.5.0]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:914) ~[spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) ~[spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) ~[spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) ~[spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:575) [javax.j2ee.servlet.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) [javax.j2ee.servlet.jar:na]

我的控制器:

package com.sdl.webapp.main.controller;

import com.sdl.webapp.common.api.WebRequestContext;
import com.sdl.webapp.common.api.localization.Localization;
import com.sdl.webapp.common.api.localization.LocalizationResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

@Controller
public class AdminController
{
  private static final Logger LOG = LoggerFactory.getLogger(AdminController.class);
  private String successView;
  @Autowired
  private WebRequestContext webRequestContext;
  @Autowired
  private LocalizationResolver localizationResolver;

  public AdminController() {}

  @RequestMapping(method=RequestMethod.GET, value="/.well-known/apple-app-site-association", headers="content-type=application/json", produces = "application/json", consumes = "application/json")
  public String getHeaders(@RequestHeader(value="Content-Type", required=false, defaultValue="application/json") String ContentType, @RequestHeader(value="Accept", defaultValue="application/json") String accept)
  {
    Localization localization = webRequestContext.getLocalization();

    LOG.trace("ContentType : " + ContentType);

    return localization.getPath() + "/.well-known/apple-app-site-association";
  }
}

标签: javaspringspring-mvc

解决方案


将产品类型添加到@RequestMapping()

例子 :

produces = MediaType.APPLICATION_JSON_VALUE
produces = MediaType.TEXT_PLAIN_VALUE
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE}

推荐阅读