Can't send json response on POST request in Spring MVC controller












0















I have no trouble to send list of objects in response on POST request and receive it in VueJs client



@RequestMapping(value={"/data/parts"}, method = RequestMethod.POST)
@ResponseBody public List<Part> getPartsList( @RequestBody LookupForm lookupForm ) {
return getService().findParts(lookupForm.getCode(), lookupForm.getName(), lookupForm.getWarehouseCode());
}


But when I try to response with custom class Response (I even added produces="application/json" in RequestMapping annotation )



@RequestMapping(value={"/addPartsRequest"}, method = {RequestMethod.POST}, produces="application/json")
@ResponseBody public Response addPartsRequest(@RequestBody PartsRequest partsRequest) {
Response response = new Response("Fail","Your Request failed");
PartsRequest newRequest = getService().addPartsRequest(partsRequest);
if (newRequest != null){
response = new Response("Ok", "The Ticket has been submitted.");
}
return response;
}

class Response {
String message;
String status;

public Response() {
// empty c-tor for serialization
}

public Response(String status, String message) {
this.message = message;
this.status = status;
}
// ... getters & setters omitted
}


On VueJs side request sent with help of axios.post



var headers = {
'Content-Type': 'application/json'
}
axios.post(`${API_URL.orderApi}`, formData, {headers})
.then(response => {
const commitPayload = response.data.message;
const status = response.data.status;
if(status === 'Ok'){
this.$store.commit('setMessage', commitPayload)
}else{
this.$store.commit('setErrMessage', commitPayload)
}
this.$router.push('/parts')
},
error => {
this.$store.commit('setErrMessage', 'Submit Order failed')
this.$router.push('/parts')
})


Vue UI client receive



Status Code: 406 Not Acceptable
Content-Type: text/html;charset=UTF-8
<html><head><title>Error</title></head><body>Not Acceptable</body></html>


Why I can respond with list of object and can't respond with POJO and how to fix the issue?
Thank you.



PS. Project depends on jackson-databind v2.8.2 and spring v4.3.1










share|improve this question





























    0















    I have no trouble to send list of objects in response on POST request and receive it in VueJs client



    @RequestMapping(value={"/data/parts"}, method = RequestMethod.POST)
    @ResponseBody public List<Part> getPartsList( @RequestBody LookupForm lookupForm ) {
    return getService().findParts(lookupForm.getCode(), lookupForm.getName(), lookupForm.getWarehouseCode());
    }


    But when I try to response with custom class Response (I even added produces="application/json" in RequestMapping annotation )



    @RequestMapping(value={"/addPartsRequest"}, method = {RequestMethod.POST}, produces="application/json")
    @ResponseBody public Response addPartsRequest(@RequestBody PartsRequest partsRequest) {
    Response response = new Response("Fail","Your Request failed");
    PartsRequest newRequest = getService().addPartsRequest(partsRequest);
    if (newRequest != null){
    response = new Response("Ok", "The Ticket has been submitted.");
    }
    return response;
    }

    class Response {
    String message;
    String status;

    public Response() {
    // empty c-tor for serialization
    }

    public Response(String status, String message) {
    this.message = message;
    this.status = status;
    }
    // ... getters & setters omitted
    }


    On VueJs side request sent with help of axios.post



    var headers = {
    'Content-Type': 'application/json'
    }
    axios.post(`${API_URL.orderApi}`, formData, {headers})
    .then(response => {
    const commitPayload = response.data.message;
    const status = response.data.status;
    if(status === 'Ok'){
    this.$store.commit('setMessage', commitPayload)
    }else{
    this.$store.commit('setErrMessage', commitPayload)
    }
    this.$router.push('/parts')
    },
    error => {
    this.$store.commit('setErrMessage', 'Submit Order failed')
    this.$router.push('/parts')
    })


    Vue UI client receive



    Status Code: 406 Not Acceptable
    Content-Type: text/html;charset=UTF-8
    <html><head><title>Error</title></head><body>Not Acceptable</body></html>


    Why I can respond with list of object and can't respond with POJO and how to fix the issue?
    Thank you.



    PS. Project depends on jackson-databind v2.8.2 and spring v4.3.1










    share|improve this question



























      0












      0








      0








      I have no trouble to send list of objects in response on POST request and receive it in VueJs client



      @RequestMapping(value={"/data/parts"}, method = RequestMethod.POST)
      @ResponseBody public List<Part> getPartsList( @RequestBody LookupForm lookupForm ) {
      return getService().findParts(lookupForm.getCode(), lookupForm.getName(), lookupForm.getWarehouseCode());
      }


      But when I try to response with custom class Response (I even added produces="application/json" in RequestMapping annotation )



      @RequestMapping(value={"/addPartsRequest"}, method = {RequestMethod.POST}, produces="application/json")
      @ResponseBody public Response addPartsRequest(@RequestBody PartsRequest partsRequest) {
      Response response = new Response("Fail","Your Request failed");
      PartsRequest newRequest = getService().addPartsRequest(partsRequest);
      if (newRequest != null){
      response = new Response("Ok", "The Ticket has been submitted.");
      }
      return response;
      }

      class Response {
      String message;
      String status;

      public Response() {
      // empty c-tor for serialization
      }

      public Response(String status, String message) {
      this.message = message;
      this.status = status;
      }
      // ... getters & setters omitted
      }


      On VueJs side request sent with help of axios.post



      var headers = {
      'Content-Type': 'application/json'
      }
      axios.post(`${API_URL.orderApi}`, formData, {headers})
      .then(response => {
      const commitPayload = response.data.message;
      const status = response.data.status;
      if(status === 'Ok'){
      this.$store.commit('setMessage', commitPayload)
      }else{
      this.$store.commit('setErrMessage', commitPayload)
      }
      this.$router.push('/parts')
      },
      error => {
      this.$store.commit('setErrMessage', 'Submit Order failed')
      this.$router.push('/parts')
      })


      Vue UI client receive



      Status Code: 406 Not Acceptable
      Content-Type: text/html;charset=UTF-8
      <html><head><title>Error</title></head><body>Not Acceptable</body></html>


      Why I can respond with list of object and can't respond with POJO and how to fix the issue?
      Thank you.



      PS. Project depends on jackson-databind v2.8.2 and spring v4.3.1










      share|improve this question
















      I have no trouble to send list of objects in response on POST request and receive it in VueJs client



      @RequestMapping(value={"/data/parts"}, method = RequestMethod.POST)
      @ResponseBody public List<Part> getPartsList( @RequestBody LookupForm lookupForm ) {
      return getService().findParts(lookupForm.getCode(), lookupForm.getName(), lookupForm.getWarehouseCode());
      }


      But when I try to response with custom class Response (I even added produces="application/json" in RequestMapping annotation )



      @RequestMapping(value={"/addPartsRequest"}, method = {RequestMethod.POST}, produces="application/json")
      @ResponseBody public Response addPartsRequest(@RequestBody PartsRequest partsRequest) {
      Response response = new Response("Fail","Your Request failed");
      PartsRequest newRequest = getService().addPartsRequest(partsRequest);
      if (newRequest != null){
      response = new Response("Ok", "The Ticket has been submitted.");
      }
      return response;
      }

      class Response {
      String message;
      String status;

      public Response() {
      // empty c-tor for serialization
      }

      public Response(String status, String message) {
      this.message = message;
      this.status = status;
      }
      // ... getters & setters omitted
      }


      On VueJs side request sent with help of axios.post



      var headers = {
      'Content-Type': 'application/json'
      }
      axios.post(`${API_URL.orderApi}`, formData, {headers})
      .then(response => {
      const commitPayload = response.data.message;
      const status = response.data.status;
      if(status === 'Ok'){
      this.$store.commit('setMessage', commitPayload)
      }else{
      this.$store.commit('setErrMessage', commitPayload)
      }
      this.$router.push('/parts')
      },
      error => {
      this.$store.commit('setErrMessage', 'Submit Order failed')
      this.$router.push('/parts')
      })


      Vue UI client receive



      Status Code: 406 Not Acceptable
      Content-Type: text/html;charset=UTF-8
      <html><head><title>Error</title></head><body>Not Acceptable</body></html>


      Why I can respond with list of object and can't respond with POJO and how to fix the issue?
      Thank you.



      PS. Project depends on jackson-databind v2.8.2 and spring v4.3.1







      json spring-mvc vue.js jackson jackson-databind






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 19:24







      AlexeiP

















      asked Jan 18 at 18:40









      AlexeiPAlexeiP

      841414




      841414
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I get the response properly when I do it from postman. I believe you have getters and setters.



          use the header Accept: application/json



          Mark the class as implements serializable.






          share|improve this answer
























          • Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

            – AlexeiP
            Jan 18 at 20:30











          • Did you try the header Accept: application/json in VueJs

            – suresh
            Jan 18 at 22:02











          • I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

            – AlexeiP
            Jan 19 at 17:04











          • Do you have these dependencies added. <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>

            – suresh
            Jan 20 at 19:10













          • Thanks for help. I found reason and post answer.

            – AlexeiP
            Jan 21 at 16:26



















          0














          It's turns out that I was sending POST request with json body to controller mapped to url with suffix 'htm'. This request cause conflict with mime-mapping 'text/html' and as result server immediately response with code 406.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259751%2fcant-send-json-response-on-post-request-in-spring-mvc-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I get the response properly when I do it from postman. I believe you have getters and setters.



            use the header Accept: application/json



            Mark the class as implements serializable.






            share|improve this answer
























            • Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

              – AlexeiP
              Jan 18 at 20:30











            • Did you try the header Accept: application/json in VueJs

              – suresh
              Jan 18 at 22:02











            • I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

              – AlexeiP
              Jan 19 at 17:04











            • Do you have these dependencies added. <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>

              – suresh
              Jan 20 at 19:10













            • Thanks for help. I found reason and post answer.

              – AlexeiP
              Jan 21 at 16:26
















            0














            I get the response properly when I do it from postman. I believe you have getters and setters.



            use the header Accept: application/json



            Mark the class as implements serializable.






            share|improve this answer
























            • Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

              – AlexeiP
              Jan 18 at 20:30











            • Did you try the header Accept: application/json in VueJs

              – suresh
              Jan 18 at 22:02











            • I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

              – AlexeiP
              Jan 19 at 17:04











            • Do you have these dependencies added. <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>

              – suresh
              Jan 20 at 19:10













            • Thanks for help. I found reason and post answer.

              – AlexeiP
              Jan 21 at 16:26














            0












            0








            0







            I get the response properly when I do it from postman. I believe you have getters and setters.



            use the header Accept: application/json



            Mark the class as implements serializable.






            share|improve this answer













            I get the response properly when I do it from postman. I believe you have getters and setters.



            use the header Accept: application/json



            Mark the class as implements serializable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 18 at 20:25









            sureshsuresh

            1411111




            1411111













            • Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

              – AlexeiP
              Jan 18 at 20:30











            • Did you try the header Accept: application/json in VueJs

              – suresh
              Jan 18 at 22:02











            • I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

              – AlexeiP
              Jan 19 at 17:04











            • Do you have these dependencies added. <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>

              – suresh
              Jan 20 at 19:10













            • Thanks for help. I found reason and post answer.

              – AlexeiP
              Jan 21 at 16:26



















            • Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

              – AlexeiP
              Jan 18 at 20:30











            • Did you try the header Accept: application/json in VueJs

              – suresh
              Jan 18 at 22:02











            • I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

              – AlexeiP
              Jan 19 at 17:04











            • Do you have these dependencies added. <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>

              – suresh
              Jan 20 at 19:10













            • Thanks for help. I found reason and post answer.

              – AlexeiP
              Jan 21 at 16:26

















            Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

            – AlexeiP
            Jan 18 at 20:30





            Yes I mark getters and setters omitted in code snippet for clarity. And I added class Response implements Serializable after I post question. Still not working.

            – AlexeiP
            Jan 18 at 20:30













            Did you try the header Accept: application/json in VueJs

            – suresh
            Jan 18 at 22:02





            Did you try the header Accept: application/json in VueJs

            – suresh
            Jan 18 at 22:02













            I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

            – AlexeiP
            Jan 19 at 17:04





            I really don't understand how this ` Accept: application/json in VueJs` going to improve situation if server already responds with 406 Not Acceptable? i.e. client side already receive error regardless it ready to accept json.

            – AlexeiP
            Jan 19 at 17:04













            Do you have these dependencies added. <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>

            – suresh
            Jan 20 at 19:10







            Do you have these dependencies added. <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>

            – suresh
            Jan 20 at 19:10















            Thanks for help. I found reason and post answer.

            – AlexeiP
            Jan 21 at 16:26





            Thanks for help. I found reason and post answer.

            – AlexeiP
            Jan 21 at 16:26













            0














            It's turns out that I was sending POST request with json body to controller mapped to url with suffix 'htm'. This request cause conflict with mime-mapping 'text/html' and as result server immediately response with code 406.






            share|improve this answer




























              0














              It's turns out that I was sending POST request with json body to controller mapped to url with suffix 'htm'. This request cause conflict with mime-mapping 'text/html' and as result server immediately response with code 406.






              share|improve this answer


























                0












                0








                0







                It's turns out that I was sending POST request with json body to controller mapped to url with suffix 'htm'. This request cause conflict with mime-mapping 'text/html' and as result server immediately response with code 406.






                share|improve this answer













                It's turns out that I was sending POST request with json body to controller mapped to url with suffix 'htm'. This request cause conflict with mime-mapping 'text/html' and as result server immediately response with code 406.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 21 at 16:24









                AlexeiPAlexeiP

                841414




                841414






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259751%2fcant-send-json-response-on-post-request-in-spring-mvc-controller%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre