使用SpringMVC搭建Rest服务(4)

信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7736bd: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springConfig,studentManager,studentServer,jaxb2Marshaller,annotationMethodHandlerAdapter]; root of factory hierarchy

2011-6-24 21:22:08 org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromClasses

信息: Creating JAXBContext with classes to be bound [class com.upc.upcgrid.guan.springMvcRestTest.bean.student.Student,class com.upc.upcgrid.guan.springMvcRestTest.bean.student.StudentList]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students/{id}] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students/{id}.*] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students/{id}/] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students.*] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

信息: Mapped URL path [/students/] onto handler [com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]

2011-6-24 21:22:09 org.springframework.web.servlet.FrameworkServlet initServletBean

信息: FrameworkServlet 'rest': initialization completed in 969 ms

2011-06-24 21:22:09.125:INFO::Started SelectChannelConnector@202.194.158.128:8586

2011-06-24 21:22:09.125:INFO::Started SelectChannelConnector@localhost:8585

从输出的内容可以看出,Spring服务已经正常启动,并且Jetty将会在本地的8586和8585两个端口进行监听。

7.  编写客户端测试程序

Rest的客户端可以使用Spring的RestTemplate进行编写。为了给使用这屏蔽掉我们后台与服务器进行通信的复杂操作,我们编写了一个RestUtility,这个类主要完成与服务器的通信。

RestUtility.java

public class RestUtility {

private static String url = "http://202.194.158.128:8586/SpringMVCRestTest/rest/students/";   

private static RestTemplate restTemplate;

static {

createRestTemplate();

}

public static RestTemplate getRestTemplate(){   

return restTemplate;

}

private static void createRestTemplate() {

restTemplate = new RestTemplate();

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();//消息体转换器列表

MarshallingHttpMessageConverter marshalConverter = new MarshallingHttpMessageConverter();//xom类型的消息体转换器

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();//创建JAXB2类型的xom环境

marshaller.setClassesToBeBound(Student.class,StudentList.class);//将类绑定到JAXB2

marshalConverter.setMarshaller(marshaller);//设置编组器

marshalConverter.setUnmarshaller(marshaller);//设置解组器

converters.add(marshalConverter);//将xom消息体转换器添加到列表 

converters.add(new StringHttpMessageConverter());

restTemplate.setMessageConverters(converters);//将转换器列表放入RestTemplate

}

public static void addStudent(Student student) throws RestClientException, URISyntaxException{

System.out.println(restTemplate.postForObject(new URI(url), student,String.class));

}

public static Student getStudent(String id){

return restTemplate.getForObject(url+"{id}", Student.class, id);

}

public static void updateStudent(String id,Student student){

restTemplate.put(url+"{id}", student, id);

}

public static void deleteStudent(String id){

restTemplate.delete(url+"{id}", id);

}

public static StudentList getAllStudents() throws RestClientException, URISyntaxException{

return restTemplate.getForObject(new URI(url),StudentList.class);

}

}

这个类主要使用RestTemplate对服务器进行通信的。

最后给出一个测试程序,并给出结果.

public class RestClient {

public static void main(String[] args) throws RestClientException, URISyntaxException {

Student student = new Student();

student.setAge(20);

student.setName("Mary");

student.setId("05080416"); 

RestUtility.addStudent(student);      

student.setAge(21);

student.setName("Lucy");

student.setId("05080411");

RestUtility.addStudent(student);

student = RestUtility.getStudent("05080416");

System.err.println(student.getName());

StudentList sl = RestUtility.getAllStudents();

for(Student s : sl.getStudents())

{

System.err.println(s.getName());

}

}