2017-10-16 52 views
0

我正在處理Spring 4.2 MVC應用程序。這裏我試圖調用一個應該返回json數據的.html擴展url。問題是,當控制器調用特定的url時,我得到一個org.springframework.web.HttpMediaTypeNotAcceptableException。這完美的作品,如果我給.json擴展而不是.html。如何將.json映射到.html網址在春天mvc

我使用下面的罐子JSON映射

jackson-annotations-2.8.7.jar jackson-core-2.8.7.jar jackson-core-asl-1.9.13.jar jackson-databind-2.8.7.jar jackson-datatype-joda-2.8.7.jar jackson-jaxrs-json-provider-2.8.7.jar jackson-mapper-asl-1.9.13.jar jackson-module-jaxb-annotations-2.8.7.jar json-simple-1.1.jar

以下是我的基於Java的配置,而不是web.xml中

import javax.servlet.MultipartConfigElement; 
import javax.servlet.ServletRegistration; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 


public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

@Override 
protected Class<?>[] getRootConfigClasses() { 
return new Class[] { AppConfig.class }; 
} 

@Override 
protected Class<?>[] getServletConfigClasses() { 
return null; 
} 

@Override 
protected String[] getServletMappings() { 
return new String[] { "*.html", "*.json"}; 
} 

@Override 
protected void customizeRegistration(ServletRegistration.Dynamic registration) { 
registration.setMultipartConfig(getMultipartConfigElement()); 
} 

private MultipartConfigElement getMultipartConfigElement() { 
MultipartConfigElement multipartConfigElement = new MultipartConfigElement(LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD); 
return multipartConfigElement; 
} 

private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size. 
              // Beyond that size spring will throw exception. 
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part. 

private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk 

} 

我的控制器方法如下。

@RequestMapping(method = RequestMethod.POST, value = "/getDetails.html", produces = "application/json") 
@ResponseBody 
public String getDetails(@RequestBody String id, HttpServletRequest request, 
     HttpServletResponse response, Model model) { 
    logger.info("getDetails"); 
    ObjectMapper mapper = new ObjectMapper(); 
    // do something 
}  
} 

我從其中的URL被調用

   var getDetails = function() { 
        var id = { 
          "id" : $("#id").val() 
         } 
        $ 
          .ajax({ 
           type : "POST", 
           url : "../data/getDetails.html", 
           data : JSON.stringify(id), 
           contentType : "application/json; charset=utf-8", 
           mimeType: "application/json", 
           dataType : 'json', 
           success : function(data) { 
      // do something 

       } 

以下內容的Ajax調用從服務器日誌

INFO stdout:71 - 2017-10-16 12:13:53 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing POST request for [/App/data/getDetails.html]

DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing POST request for [/App/data/getDetails.html] INFO stdout:71 - 2017-10-16 12:13:53 DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /data/getDetails.html

DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /data/getDetails.html INFO stdout:71 - 2017-10-16 12:13:53 DEBUG ExceptionHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

DEBUG ExceptionHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation INFO stdout:71 - 2017-10-16 12:13:53 DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'globalExceptionController'

DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'globalExceptionController'

請幫幫忙!

+0

嘗試使用'產生=「text/plain的」' –

+0

@SachinGupta試過,不工作 – eccentricCoder

+0

@SachinGupta我甲肝更新了服務器日誌 – eccentricCoder

回答

1

@RequestMapping註釋中刪除produces標記。那麼它會工作。

1

(我沒有訪問對我的帖子你post..so評論發表評論。)

你嘗試consumes= "application/json"就像在你的註釋produces = "application/json"

而且

在你的Ajax調用,您同時使用的contentType和數據類型。您期待以json格式迴應嗎?

+0

我試過,但它不工作。 – eccentricCoder

+0

是的,我期待JSON格式響應 – eccentricCoder

+0

根據你的exception.did你嘗試生產= {MediaType.APPLICATION_JSON_VALUE}? –

1

假設您使用默認的 spring mvc config。它默認使用InternalResourceViewResolver。你需要做的是配置ContentNegotiatingViewResolver它試圖根據內容類型解析視圖。我沒有發佈一個例子,因爲你會在互聯網上找到很多。

我會建議您在編寫您的端點時閱讀REST端點並使用適當的表示法和標準。例如,在你的問題中,你有一個使用.html返回json響應的端點是非常正確的。