私はSpring 4.2 MVCアプリケーションを開発中です。ここでは、jsonデータを返すことになっている.html拡張URLを呼び出そうとしています。問題は、コントローラが特定のURLを呼び出すときにorg.springframework.web.HttpMediaTypeNotAcceptableExceptionが発生していることです。 .htmlの代わりに.jsonの拡張子を与えると、これは完全に動作します。.jsonをspring.mvcの.html urlにマップする方法
私はJSONのマッピングのために、次のjarファイルを使用しています
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
}
}
私は、サーバーログ
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
0から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呼び出しをされてい
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'
助けてください!
は「text/plainの」 ' –
@SachinGuptaは、私はあなたが保存されて@Sachinその後、 – eccentricCoder
@SachinGuptaを働いていないことをしようとした '=を生産する使用しよう私の一日。私が受け入れることができるように、回答者として投稿してください。 – eccentricCoder