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

2

8

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

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 - HttpMediaTypeNotAcceptableException where similar problem was discussed but I am not sure how to go about solving this with annotation.

Any idea?

Mohler answered 13/3, 2014 at 16:19 Comment(0)
M
6

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;
}
Mohler answered 13/3, 2014 at 16:41 Comment(3)
However small it is. Never the less it is a valuable question with valuable answer. I have it correct at some place and I just forgot about it. This helped me to find my mistake quickly. So do not ignore this question. Thanks!!Bane
@user1647708: So what if my return type is List<String>? what should I return?Sepulcher
@VenkataRamireddyCH ResponseEntity.noContent().build();Imperceptible
R
1

u can try this:

@RequestMapping(value = "audit/unaudit", method = RequestMethod.GET,produces = "application/json")
Remotion answered 9/12, 2016 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.