Home > Mobile >  Spring REST API Could not find acceptable representation error status 406
Spring REST API Could not find acceptable representation error status 406

Time:11-05

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&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:315)&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219)&#xd;
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)&#xd;
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)&#xd;
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)&#xd;
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)&#xd;
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)&#xd;
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)&#xd;
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:670)&#xd;
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)&#xd;
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)&#xd;
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)&#xd;
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)&#xd;
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)&#xd;
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)&#xd;
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)&#xd;
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)&#xd;
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)&#xd;
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)&#xd;
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)&#xd;
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)&#xd;
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)&#xd;
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)&#xd;
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)&#xd;
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)&#xd;
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)&#xd;
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)&#xd;
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)&#xd;
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)&#xd;
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)&#xd;
    at java.base/java.lang.Thread.run(Thread.java:833)&#xd;
</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.

CodePudding user response:

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);
    }
  • Related