1. 程式人生 > >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

Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.jersey.test.entity.Student]
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);
   }