[英]415 Unsupported Media Type AngularJS to SpringMVC Controller
尝试将JSON数据从angularjs控制器发布到SpringMVC控制器时遇到此错误。 我已经尝试过这里发布的许多解决方案,以及网上其他可用的东西。 我的类路径中已经有杰克逊图书馆。 而且由于互联网问题,我不使用Maven。
SpringMVC控制器
@Controller
public class MainController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping(value = "/employee", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
String saveEmployee(@RequestBody Employee employee) {
//Will do some stuff here.
System.out.println("INSIDE CONTROLLER");
StringBuilder json = new StringBuilder();
return json.toString();
}
}
AngularJS控制器
app.controller('saveEmployeeCtrl', function ($scope, $http) {
$scope.employee = {};
$scope.saveEmployee = function () {
$http({
method: 'POST',
url: 'employee',
data: $scope.employee,
headers:{'Accept':'application/json', 'Content': 'application/json'}
}).success(function(data){
console.log('something nice');
});
};
});
Web配置
@EnableWebMvc
@Configuration
@ComponentScan("springmvc.com.")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webapp/resources/static/app/**")
.addResourceLocations("/webapp/resources/static/app/");
registry.addResourceHandler("/webapp/resources/static/lib/**")
.addResourceLocations("/webapp/resources/static/lib/");
registry.addResourceHandler("/webapp/resources/static/js/**")
.addResourceLocations("/webapp/resources/static/js/");
registry.addResourceHandler("/webapp/resources/static/css/**")
.addResourceLocations("/webapp/resources/static/css/");
registry.addResourceHandler("/webapp/webapp/resources/static/views/**")
.addResourceLocations("/webapp/webapp/resources/static/views/");
registry.addResourceHandler("/webapp/resources/static/**")
.addResourceLocations("/webapp/resources/static/");
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(true)
.parameterName("mediaType").ignoreAcceptHeader(true)
.useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebAppInitializer
public class WebAppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "springmvc.com.config";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("***** Initializing Application for " + servletContext.getServerInfo() + " *****");
// Create ApplicationContext
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setConfigLocation(CONFIG_LOCATION);
// Add the servlet mapping manually and make it initialize automatically
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);
servlet.addMapping("/");
servlet.setAsyncSupported(true);
servlet.setLoadOnStartup(1);
}
}
最重要的是服务器应用程序的日志文件中显示的内容。 引发什么异常是导致此http状态的原因。 因此,如果上述解决方案对您没有帮助,请检查日志(也许增加春季的日志级别)并将其发布在此处。
更新:
我还有几个其他问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.