我已经创建了一个简单的带有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.xml
或http://localhost:8080/spring-ws/studentlist.htm
,那么我将收到以下错误消息:
由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。
您能帮我解决这个问题吗?
另外ignoreAcceptHeader
,我的代码中property的用途是什么?总是需要吗?
我已经提到了这个SO帖子:带有JSON的Spring RESTful Web服务给出了HTTP 406错误代码
如以上文章所述,在使用Chrome PostMan客户端提交请求时,我已经尝试过将Accept
标头设置为application/xml
和text/html
,但是仍然看到相同的问题。
您试图返回List
的Student
在你的控制器对象。
因此,请创建带有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] 删除。
我来说两句