如何将.json映射到.html网址在春天mvc
我正在处理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'
请帮帮忙!
从@RequestMapping
注释中删除produces
标记。那么它会工作。
(我没有访问对我的帖子你post..so评论发表评论。)
你尝试consumes= "application/json"
就像在你的注释produces = "application/json"
?
而且
在你的Ajax调用,您同时使用的contentType和数据类型。您期待以json格式回应吗?
我试过,但它不工作。 – eccentricCoder
是的,我期待JSON格式响应 – eccentricCoder
根据你的exception.did你尝试生产= {MediaType.APPLICATION_JSON_VALUE}? –
假设您使用默认的 spring mvc config。它默认使用InternalResourceViewResolver。你需要做的是配置ContentNegotiatingViewResolver它试图根据内容类型解析视图。我没有发布一个例子,因为你会在互联网上找到很多。
我会建议您在编写您的端点时阅读REST端点并使用适当的表示法和标准。例如,在你的问题中,你有一个使用.html返回json响应的端点是非常正确的。
尝试使用'产生=“text/plain的”' –
@SachinGupta试过,不工作 – eccentricCoder
@SachinGupta我甲肝更新了服务器日志 – eccentricCoder