Java Spring mvc 抛出 org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22384722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:26:39  来源:igfitidea点击:

Spring mvc throwing org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

javaspringspring-mvc

提问by user1647708

I am using springMVC and I am getting following exception when trying to do an update.

我正在使用 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'

following is the controller method that throws the exception. Is there something that I need to do to make this work?

以下是引发异常的控制器方法。有什么我需要做的事情吗?

@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;
}

I found following post Spring MVC - HttpMediaTypeNotAcceptableExceptionwhere similar problem was discussed but I am not sure how to go about solving this with annotation.

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

Any idea?

任何的想法?

回答by user1647708

The exception was thrown because of the return value of the controller. Once I changed the return value the exception went away.

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

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

I also changed the response to be 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;
}

回答by Sherry Su

u can try this:

你可以试试这个:

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