1

I am building a web app in spring MVC.

I want to retrieve List of patient (collection) using Ajax. But it throws me error of 406 not acceptable

Jsp File

$(document).ready(function () {
                    $.ajax({
                        url: 'searchPatient',
                        //data: "uhid=" + $("#uhid").val() + "&type=" + $("#type").val(),
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            alert(data);
                        }
                    });
                });

** Controller File

@RequestMapping("/searchPatient")
    public @ResponseBody List<String> getPatient() 
    {
             List<String> s = new ArrayList<String>();
        s.add("hello");
        return s;
    }

Console View

How can I solve this error?

Keval Pithva
  • 600
  • 2
  • 5
  • 21
  • see if [this answer](http://stackoverflow.com/questions/26825276/spring-4-restcontroller-json-characteristics-not-acceptable-according-to-the-re/26826136#26826136) helps – Master Slave Apr 01 '16 at 05:21
  • try removing `contentType` since you are not sending any data to server and for this server rejects the request. Also GET request should not have a request body. It will be rejected by most application server. refer for [406 HTTP status](http://www.checkupdown.com/status/E406.html) – Ekansh Rastogi Apr 01 '16 at 06:46
  • I have changed it to Post request but still not working – Keval Pithva Apr 01 '16 at 08:08

2 Answers2

0

your request URI is http://…/HMIS/searchPatient, so make sure your DispatcherServlet is mapped to /HMIS/* or it's existed in your controller @RequestMapping

Pianov
  • 1,533
  • 9
  • 16
0

I know this is an old question, but I also had this error an solved it adding the definition for MappingJacksonHttpMessageConverter like this:

<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<!-- Bind the return value of the Rest service to the ResponseBody. -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list id="beanList">
            <ref bean="jsonHttpMessageConverter" />          
        </util:list>
    </property>
</bean>

Link to the original answer hope it helps someone else.

maxivis
  • 1,727
  • 3
  • 21
  • 35