2016-10-01 98 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全部」版本,工作得很好。 –

相關問題