SpringMVC restful 异常Could not write request: no suitable HttpMessageConverter found for request type
来源:互联网 发布:网络共享打印机挂起 编辑:程序博客网 时间:2024/06/05 08:14
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279)
at com.jersey.test.RestClient.addStudent(RestClient.java:47)
at com.jersey.test.RestClient.main(RestClient.java:75)
springMVC controller 如下:
@RequestMapping(value="/static/restful")
@Controller
public class StudentRestfulController {
private IBaseService<Student> studentService;
@RequestMapping(value="/addStudent",method=RequestMethod.POST)
public void addStudent(@RequestBody Student entity){
//this.studentService.save(entity);
System.out.println("firstname :"+entity.getFirstname()+" lastname "+entity.getLastname());
}
@RequestMapping(value="/batchAddStudent",method=RequestMethod.POST)
public void batchAddStudent(@RequestBody List<Student> students){
if(students!=null&&students.size()>0){
for(Student student:students){
this.studentService.save(student);
}
}
}
}
实体:
public class Student {
private String workUnit = null;
private String id = null;
private String loginname = null;
private String password = null;
private String lastname = null;
}
客户端调用代码:
public class RestClient {
private RestTemplate template;
private final static String url = "http://localhost:8080/rdb/static/restful/";
public void addStudent(Student stu){
template=new RestTemplate();
template.postForObject(url+"addStudent.do", stu, String.class);
}
}
解决方法:
一、在实体类中加上@XmlRootElement注解,SpringMVC就可以解析出来了
二、修改客户端中调用的代码
在RestTemplate模板中添加消息转换器,有两种方法
1.直接在代码中添加
public void addStudent(Student stu){
template=new RestTemplate();
List messageConverters=new ArrayList();
messageConverters.add(new SourceHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new MappingJacksonHttpMessageConverter());
template.setMessageConverters(messageConverters);
template.postForObject(url+"addStudent.do", stu, String.class);
}
2.在客户端XML配置文件中添加转换器
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property></bean>
测试
public void testRest(Student stu){ BeanFactory bean=new ClassPathXmlApplicationContext("applicationContext.xml"); template=(RestTemplate)bean.getBean("restTemplate"); template.postForObject(url+"addStudent.do", stu, String.class); }
- SpringMVC restful 异常Could not write request: no suitable HttpMessageConverter found for request type
- SpringMVC restful 异常Could not write request: no suitable HttpMessageConverter found for request type
- RestTemplate报错:no suitable HttpMessageConverter found for request type
- JsonMappingException: No suitable constructor found for type
- HttpMessageNotWritableException: Could not write JSON: No serializer found for class ****
- Springmvc 请求转发 No mapping found for HTTP request
- mark springmvc [No mapping found for HTTP request with URI]
- springMVC No mapping found for HTTP request with URI
- springMVC No mapping found for HTTP request with URI
- springMVC No mapping found for HTTP request with URI
- springMVC No mapping found for HTTP request with URI
- springMVC--关于no mapping found for http request with uri
- springMVC--关于no mapping found for http request with uri .......
- springMVC No mapping found for HTTP request with URI
- SpringMVC出现no mapping found for http request的解决办法
- springMVC No mapping found for HTTP request with URI
- springmvc 出现No mapping found for HTTP request with URI
- HttpMessageConverter配置javaconfig(未配置异常:No converter found for return value of type)
- ajax post 异步 和 Json ashx学习
- linux常用命令
- 委托和匿名委托的比较
- HTML5学习(5) 新增input type
- Moving from Java to C++
- SpringMVC restful 异常Could not write request: no suitable HttpMessageConverter found for request type
- AVD(android virtual device)路径设置
- Linux设备模型 (1)
- 用VC++建立Service服务应用程序
- 使用GET的方式调用WebServices
- 几个重要的算法设计技术
- 睡前用电脑会有5大危害
- 通过后台采集返回页面信息需要注意的事项
- MFC中对文件的操作方法(转)