【问题标题】:Spring mvc throwing org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representationSpring mvc 抛出 org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
【发布时间】:2014-04-18 13:08:10
【问题描述】:

我正在使用 springMVC,并且在尝试进行更新时出现以下异常。

10:10:49,847 DEBUG LogicalConnectionImpl:250 - Released JDBC connection
10:10:49,859 DEBUG FixedContentNegotiationStrategy:48 - Requested media types is text/html (based on     default MediaType)
10:10:49,929 DEBUG ExceptionHandlerExceptionResolver:132 - Resolving exception from handler [public   com.model.JobQueue com.controller.TestResultController.updateJob(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
10:10:49,937 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [public com.model.JobQueue com.controller.TestResultController.updateJob(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
10:10:49,938 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [public com.model.JobQueue com.controller.TestResultController.updateJob(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
10:10:49,940 DEBUG DispatcherServlet:999 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
10:10:49,940 DEBUG DispatcherServlet:966 - Successfully completed request
10:10:49,941 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'

以下是引发异常的控制器方法。我需要做些什么来完成这项工作吗?

@RequestMapping(value="/updateJob", method=RequestMethod.GET)
public @ResponseBody JobQueue updateJob(@RequestParam(value="job_id") String job_id, @RequestParam String test_id, @RequestParam(value="status") String status) {
   JobQueue job = jobqueueService.getJob(Integer.parseInt(job_id));
   job.setTest_id(test_id);
   job.setStatus(Integer.parseInt(status));
   jobqueueService.updateJob(job);
   return job;
}

我发现以下帖子 Spring MVC - HttpMediaTypeNotAcceptableException 讨论了类似的问题,但我不确定如何使用注释解决此问题。

有什么想法吗?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    由于控制器的返回值引发了异常。 一旦我更改了返回值,异常就消失了。

    public @ResponseBody String updateJob(@RequestParam(value="job_id") String job_id){
    

    我还将响应更改为 null。

    @RequestMapping(value="/updateJob", method=RequestMethod.GET)
    public @ResponseBody String updateJob(@RequestParam(value="job_id") String job_id){
        Integer jobid = Integer.parseInt(job_id);
    
        JobQueue job = jobqueueService.getJob(jobid);
        .
        .
        return null;
    }
    

    【讨论】:

    • 不管它有多小。无论如何,这是一个有价值的问题,有价值的答案。我在某个地方是正确的,我只是忘记了。这帮助我快速找到错误。所以不要忽视这个问题。谢谢!!
    • @user1647708: 那么如果我的返回类型是 List 呢?我应该返回什么?
    • @VenkataRamireddyCH ResponseEntity.noContent().build();
    【解决方案2】:

    你可以试试这个:

    @RequestMapping(value = "audit/unaudit", method = RequestMethod.GET,produces = "application/json")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2013-08-08
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多