2016-10-01 94 views
0

当我试图从angularjs控制器发布JSON数据到SpringMVC控制器时出现这个错误。我已经尝试了很多在这里发布的解决方案以及其他一些网络上可用的东西。我的classpath中已经有了jackson库。而且因为互联网问题,我也没有使用maven。415不支持的媒体类型AngularJS到SpringMVC控制器

控制器用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'); 
     }); 
    }; 
}); 

WebConfig

@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); 
    } 
} 
+0

您能向我们展示'$ scope.employee'数据和员工类吗? – hurricane

回答

0
  1. 您发送标题为“内容”,但你应该发送“的Content-Type”
  2. 你发送完全相同的字段JSON的人数是Employee类,检查是否有没有额外的字段,因为杰克逊已经设置,如果无法识别的字段设置失败。并且这个问题有一些解决方案(像你班上的注释或者改变这个设置)

最重要的是什么出现在你的服务器应用程序的日志文件中。引发此http状态的原因有哪些例外。所以我上面的解决方案不帮你,请检查日志(可能会增加日志级别的春天),并在这里发布。

UPDATE:

我有一些其他问题:

  • 有你的Employee类有默认值(非参数)构造函数或也许你只能创建带参数的构造函数?你可以发布你的Employee类吗?
  • 你有没有附加到你的项目的任何记录器,日志文件中是否有任何东西(如果有的话,请张贴它)?
+0

第1步不幸没有工作。员工有确切的2个领域。我在想,因为我对这个Rest的东西完全陌生,是否必须在Employee类中加入某种Jackson注解? –

+0

不,不需要在Employee.class上放置任何Jackson注解。默认情况下,杰克逊应该从盒子中“JSON化”整个对象。 –

+0

谢谢。问题实际上是杰克逊图书馆。我下载了“1.9.9全部”版本,工作得很好。 –

相关问题