java - Spring Webservices gives 406 error -


i have created simple spring webservices example when try response getting error :

http status 406 -

the resource identified request capable of generating responses characteristics not acceptable according request "accept" headers.

this spring controller:

@controller public class datacontroller {      @requestmapping("studentlist")     public @responsebody     list<student> getstudentlist() {         list<student> studentlist = new arraylist<student>();         studentlist.add(new student(2, "a1", "b1", "a@gmail.com", "123456"));         studentlist.add(new student(3, "a2", "b2", "b@gmail.com", "123456"));         studentlist.add(new student(4, "a3", "b3", "c@gmail.com", "123456"));          return studentlist;     } } 

this student.java file:

@xmlrootelement public class student {      int id;     string firstname;     string lastname;     string email;     string mobilenumber;  // setters & getters } 

my spring web configuration file has below entry:

    <context:component-scan base-package="com.web.controller" />      <mvc:annotation-driven />  <bean     class="org.springframework.web.servlet.view.contentnegotiatingviewresolver">     <property name="mediatypes">         <map>             <entry key="json" value="application/json" />             <entry key="xml" value="text/xml" />                             <entry key="htm" value="text/html" />         </map>     </property>     <property name="defaultcontenttype" value="text/html" />       <property name="defaultviews">     <list>       <!-- json view -->       <bean         class="org.springframework.web.servlet.view.json.mappingjackson2jsonview">       </bean>        <!-- jaxb xml view -->       <bean class="org.springframework.web.servlet.view.xml.marshallingview">         <constructor-arg>             <bean class="org.springframework.oxm.jaxb.jaxb2marshaller">                <property name="classestobebound">                 <list>                    <value>com.web.domain.student</value>                 </list>                </property>             </bean>         </constructor-arg>       </bean>      </list>   </property>   <property name="ignoreacceptheader" value="true" />      </bean> 

as per settings understand if url ends .json json response. if ends .xml, xml response. if url ends .htm, html response.

if hit url http://localhost:8080/spring-ws/studentlist.json getting json response successfully.

now if url http://localhost:8080/spring-ws/studentlist.xml or http://localhost:8080/spring-ws/studentlist.htm, getting below error message:

the resource identified request capable of generating responses characteristics not acceptable according request "accept" headers.

can please me in fixing issue?

also use of ignoreacceptheader property in code? required always?

i have referred post : spring restful web service json gives http 406 error code

as given in above post, have tried set accept header application/xml , text/html while submitting request using chrome postman client still see same issue.

you trying return list of student objects in controller.

so, create wrapper class jaxb annotations , use wrapper class while returning controller fix issue.

for example, create class this:

@xmlrootelement public class wrapperlist<t> {      private list<t> list;      public wrapperlist() {         list = new arraylist<t>();     }      public wrapperlist(list<t> list) {         this.list = list;     }      @xmlanyelement     public list<t> getitems() {         return list;     } } 

and return in class:

@requestmapping("studentlist")     public      wrapperlist<student> getstudentlist() {         list<student> studentlist = new arraylist<student>();         // add students list , put them in wrapper class          wrapperlist<student> list = new wrapperlist<student>(studentlist);         return list;     } 

also can keep configuration simple this:

 <beans>     <context:component-scan base-package="com.web.controller" />      <mvc:annotation-driven />      <bean         class="org.springframework.web.servlet.view.contentnegotiatingviewresolver">         <property name="mediatypes">             <map>                 <entry key="json" value="application/json" />                 <entry key="xml" value="text/xml" />                 <entry key="htm" value="text/html" />             </map>         </property>         <property name="defaultcontenttype" value="text/html" />         <property name="ignoreacceptheader" value="true" />     </bean> </beans> 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -