如何解决'mediaType'在Spring 3.0 MVC中不能为空错误?

时间:2018-05-03 20:32:49

标签: java spring spring-mvc

如何解决mediaType在Spring 3.0 MVC中不能为空错误?我们要求我们为移动应用实现“支持通用链接”:

文件路径应为:http://localhost:9080/.well-known/apple-app-site-association当我请求网址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";
  }
}

2 个答案:

答案 0 :(得分:0)

在" /。熟知/ apple-app-site-association"的请求映射中,您正在指定" Content-Type"的请求标头参数,但是您没有提供您的请求。

尝试以下方法签名:

Correlation-Context

public String getHeaders(@RequestHeader(value="Content-Type", required=false) String ContentType)

RequestHeader注释的可能元素是defaultValue,name,required和value。

答案 1 :(得分:0)

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

示例:

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