java - Spring mvc 抛出 org.springframework.web.HttpMediaTypeNotAcceptableException : Could not find acceptable representation

标签 java spring spring-mvc

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

下面是抛出异常的 Controller 方法。我需要做些什么才能完成这项工作吗?

@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讨论了类似的问题,但我不确定如何使用注释解决这个问题。

有什么想法吗?

最佳答案

异常是因为controller的返回值而抛出的。 一旦我更改了返回值,异常就消失了。

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

我还将响应更改为空。

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

关于java - Spring mvc 抛出 org.springframework.web.HttpMediaTypeNotAcceptableException : Could not find acceptable representation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384722/

相关文章:

Java 解析 XML 文档给出 "Content not allowed in prolog."错误

java - Spring启动2.0.0.RELEASE java.lang.ClassNotFoundException : org. springframework.aop.framework.AopProxyUtils

java - Spring MVC quartz : how to set the scheduler from the view or a html page

java - 使用 JBoss 和 Intellij 进行部署

spring-mvc - Spring 3、Hibernate 4 AutoWired sessionFactory 与通用 DAO

java - 使用 spring 3.0 MVC 尝试 IntelliJ,如何导入所需的库?

java - 让 Gson 对错误的类型抛出异常

java - 多线程 ThreadSeven 扩展了 Thread

java - TableView:当滚动到达表格底部/顶部时收到通知

java - 将 Spring 参数添加到 VSCode Debug launch.json