Spring Webservices给出406错误

学习者

我已经创建了一个简单的带有webservices的Spring示例,但是当我尝试获取响应时,却收到以下错误消息:

HTTP状态406-

由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。

这是我的Spring控制器:

@Controller
public class DataController {

    @RequestMapping("studentlist")
    public @ResponseBody
    List<Student> getStudentList() {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student(2, "A1", "B1", "[email protected]", "123456"));
        studentList.add(new Student(3, "A2", "B2", "[email protected]", "123456"));
        studentList.add(new Student(4, "A3", "B3", "[email protected]", "123456"));

        return studentList;
    }
}

这是我的Student.java文件:

@XmlRootElement
public class Student {

    int id;
    String firstName;
    String lastName;
    String email;
    String mobileNumber;

// Setters & Getters
}

我的Spring Web配置文件具有以下条目:

    <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>

按照此设置,我知道如果我的URL结尾,.json我将得到JSON响应。如果以结尾.xml,我将得到XML响应。同样,如果URL以结尾.htm,我将得到一个html响应。

如果我按URL,http://localhost:8080/spring-ws/studentlist.json那么我将成功获得JSON响应。

现在,如果我的网址是http://localhost:8080/spring-ws/studentlist.xmlhttp://localhost:8080/spring-ws/studentlist.htm,那么我将收到以下错误消息:

由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。

您能帮我解决这个问题吗?

另外ignoreAcceptHeader,我的代码property的用途是什么总是需要吗?

我已经提到了这个SO帖子:带有JSON的Spring RESTful Web服务给出了HTTP 406错误代码

如以上文章所述,在使用Chrome PostMan客户端提交请求时,我已经尝试过将Accept标头设置application/xmltext/html,但是仍然看到相同的问题。

柴坦亚

您试图返回ListStudent在你的控制器对象。

因此,请创建带有JAXB批注的包装器类,并在从控制器返回以修复该问题的同时使用该包装器类。

例如,创建这样的类:

@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;
    }
}

并在您的课程中返回:

@RequestMapping("studentlist")
    public 
    WrapperList<Student> getStudentList() {
        List<Student> studentList = new ArrayList<Student>();
        // add students to the list and put them in wrapper class 
        WrapperList<Student> list = new WrapperList<Student>(studentList);
        return list;
    }

您也可以像这样保持配置简单:

 <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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring Security部署错误

来自分类Dev

Spring 3.1.1和JSON出现错误406

来自分类Dev

Spring Security Http状态406

来自分类Dev

使用JSON将HTTP 406错误升级到Spring MVC 4.1

来自分类Dev

spring request在文件扩展名上映射http错误406

来自分类Dev

Spring Integration错误日志

来自分类Dev

RestEasy Spring集成错误

来自分类Dev

当参数以(.pl)结尾时,为什么Spring MVC @RequestMapping会为映射(/user/{username:.+})抛出406错误

来自分类Dev

java ee spring错误

来自分类Dev

非标准的Spring Boot RequestMapping返回JAXB带注释的对象时产生值返回406错误

来自分类Dev

java Spring导入错误

来自分类Dev

Spring Boot EnableJpaRepositories错误

来自分类Dev

Spring Security身份验证未给出401错误

来自分类Dev

Spring @ RestController,spring-boot发生意外错误(类型=不可接受,状态= 406)

来自分类Dev

Spring集成聚合错误

来自分类Dev

使用Spring MVC在Ajax响应中获取406错误

来自分类Dev

使用get方法从angularjs调用spring boot webservice不能正常工作给出500状态错误

来自分类Dev

生成JWT令牌时,authenticate方法在Java Spring Boot中给出内部服务器错误

来自分类Dev

当Spring之前没有出现错误时,Rails Spring / Rspec / Guard会给出错误

来自分类Dev

使用Spring 4.1实现基于JSON的REST API时出现错误406

来自分类Dev

错误406无法接受的Spring MVC 4 Extjs5 JSON响应

来自分类Dev

Spring MVC-以@ResponseBody的形式返回对象时出现问题(著名错误406,未解决)

来自分类Dev

非标准的Spring Boot RequestMapping返回JAXB带注释的对象时产生值返回406错误

来自分类Dev

启用csrf时,AngularJS和Spring Security给出405错误

来自分类Dev

hbm2ddl更新在Spring Boot中给出错误

来自分类Dev

数据从 spring java 控制器返回到 angularjs 服务时出现 406 错误

来自分类Dev

Spring boot 1.5 consul 健康检查 406 错误(HttpMediaTypeNotAcceptableException)

来自分类Dev

我的 Spring Boot 服务器返回 406 Http 错误代码

来自分类Dev

Spring boot 和 angular Test 得到错误 406

Related 相关文章

  1. 1

    Spring Security部署错误

  2. 2

    Spring 3.1.1和JSON出现错误406

  3. 3

    Spring Security Http状态406

  4. 4

    使用JSON将HTTP 406错误升级到Spring MVC 4.1

  5. 5

    spring request在文件扩展名上映射http错误406

  6. 6

    Spring Integration错误日志

  7. 7

    RestEasy Spring集成错误

  8. 8

    当参数以(.pl)结尾时,为什么Spring MVC @RequestMapping会为映射(/user/{username:.+})抛出406错误

  9. 9

    java ee spring错误

  10. 10

    非标准的Spring Boot RequestMapping返回JAXB带注释的对象时产生值返回406错误

  11. 11

    java Spring导入错误

  12. 12

    Spring Boot EnableJpaRepositories错误

  13. 13

    Spring Security身份验证未给出401错误

  14. 14

    Spring @ RestController,spring-boot发生意外错误(类型=不可接受,状态= 406)

  15. 15

    Spring集成聚合错误

  16. 16

    使用Spring MVC在Ajax响应中获取406错误

  17. 17

    使用get方法从angularjs调用spring boot webservice不能正常工作给出500状态错误

  18. 18

    生成JWT令牌时,authenticate方法在Java Spring Boot中给出内部服务器错误

  19. 19

    当Spring之前没有出现错误时,Rails Spring / Rspec / Guard会给出错误

  20. 20

    使用Spring 4.1实现基于JSON的REST API时出现错误406

  21. 21

    错误406无法接受的Spring MVC 4 Extjs5 JSON响应

  22. 22

    Spring MVC-以@ResponseBody的形式返回对象时出现问题(著名错误406,未解决)

  23. 23

    非标准的Spring Boot RequestMapping返回JAXB带注释的对象时产生值返回406错误

  24. 24

    启用csrf时,AngularJS和Spring Security给出405错误

  25. 25

    hbm2ddl更新在Spring Boot中给出错误

  26. 26

    数据从 spring java 控制器返回到 angularjs 服务时出现 406 错误

  27. 27

    Spring boot 1.5 consul 健康检查 406 错误(HttpMediaTypeNotAcceptableException)

  28. 28

    我的 Spring Boot 服务器返回 406 Http 错误代码

  29. 29

    Spring boot 和 angular Test 得到错误 406

热门标签

归档