Issue
I am trying to get data by Request Param on Spring Web, I got this error:
Error Output:
<status>406</status>
<error>Not Acceptable</error>
<trace>org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:315)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:670)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
</trace>
<message>Could not find acceptable representation</message>
<path>/api/users</path>
I tried to add these dependencies on my POM.XML, but it is not working, now I got the same erro on XML (I am trying get JSON response).
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
GET request
http://localhost:8080/api/users?userID=1
Controller
@RestController
@EnableWebMvc
@RequestMapping("/api")
@Log4j2
public class UsersController {
@Autowired
UsersService service;
@PostMapping("/users")
public Users createU(@RequestBody UsersPostRequestBody ubody) {
return service.save(ubody);
}
@ResponseBody
@GetMapping(value = "/users")
public ResponseEntity<UsersGetByIdResponseBody> findUById(@RequestParam Long userID) {
return service.getUsersInfoById(userID);
}
}
Service
@Override
public ResponseEntity<UsersGetByIdResponseBody> getUsersInfoById(Long usersID) {
Users user = repository.findById(usersID)
.orElseThrow(() -> new NullPointerException("User not found."));
UsersGetByIdResponseBody userResponse = new UsersGetByIdResponseBody(user.getUserID(), user.getUsername(), user.getReputation(),
user.getProfileImageURL(), user.getBannerImageURL(), user.getAboutMe(), user.getTag(), user.getAccountType());
return ResponseEntity.ok(userResponse);
}
UsersGetByIdResponseBody
@AllArgsConstructor
public class UsersGetByIdResponseBody {
private Long userID;
private String username;
private Long reputation;
private String profileImageURL;
private String bannerImageURL;
private String aboutMe;
private String tag;
private AccountTypeEnum accountType;
}
Output expected (A normal response with object and status OK):
{
"userID": 1,
"username": "john",
"reputation": 0,
"profileImageURL": null,
"bannerImageURL": null,
"aboutMe": null,
"tag": null,
"accountType": "DEFAULT"
}
I tried add some dependencies, but it did not solved, it just change my error message to XML. I am waiting to get the object UsersGetByIdResponseBody as my body response with status OK.
Solution
I think you need to add produces
to your GetMapping
annotation:
@ResponseBody
@GetMapping(value = "/users",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UsersGetByIdResponseBody> findUById(@RequestParam Long userID) {
return service.getUsersInfoById(userID);
}
Note you will also need this (and consumes
) for your POST handler when you test that
@PostMapping("/users",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE))
public Users createU(@RequestBody UsersPostRequestBody ubody) {
return service.save(ubody);
}
Answered By - Ben Borchard
Answer Checked By - David Goodson (JavaFixing Volunteer)