32. Java 5 / JDBC public class ContactRowMapper implements RowMapper<Contact> { @Override public Contact mapRow(ResultSet rs, int index) throws SQLException { Contact contact = new Contact(); ... return contact; } } Contact contact = jdbcTpl.queryForObject( "select * from contact where id = ?" , new ContactRowMapper(),id ); List<Contact> contacts = jdbcTpl.query( "select * from contact where nom = ?" , new ContactRowMapper(), nom ); Générique Varargs
66. Support REST / contrôleur @Controller public class ContactController { @Autowired private ContactService contactService ; @RequestMapping (value= "/contacts/{id}" ,method=RequestMethod. GET ) public ModelAndView read( @PathVariable Long id) throws Exception { Contact contact = contactService .get(id); return new ModelAndView( "default" ).addObject(contact); } } URI template ex. : http://localhost:8080/contact/services/contacts/1 Binding avec @PathVariable Opération HTTP GET Génération de la vue Spring MVC « classique »
74. REST / convertisseur réponse / contact public class ContactHttpMessageConverter implements HttpMessageConverter<Contact> { public Contact read(Class<? extends Contact> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { XStream xstream = new XStream(); xstream.alias( "contact" , Contact. class ); return (Contact) xstream.fromXML(inputMessage.getBody()); } public void write(Contact contact, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { XStream xstream = new XStream(); xstream.alias( "contact" , Contact. class ); xstream.toXML(contact, outputMessage.getBody()); } public boolean canRead(Class<?> clazz, MediaType mediaType) { return Contact. class .isAssignableFrom(clazz); } public boolean canWrite(Class<?> clazz, MediaType mediaType) { return Contact. class .isAssignableFrom(clazz); } public List<MediaType> getSupportedMediaTypes() { return Arrays. asList ( new MediaType( "application" , "xml" ), new MediaType( "text" , "xml" )); } }
75.
76.
77. ex. : convertisseur XML < bean class = "org.springframework.web.client.RestTemplate" > < property name = "messageConverters" > < list > < bean class = " org.springframework.http.converter.xml.MarshallingHttpMessageConverter" > < property name = "marshaller" ref = "marshaller" /> < property name = "unmarshaller" ref = "marshaller" /> </ bean > </ list > </ property > </ bean >
78. REST / Opérations HTTP @Controller public class ContactController { @RequestMapping (value= "/contacts/" ,method=RequestMethod. PUT ) public ModelAndView create( @RequestBody Contact contact) throws Exception { contact = contactService .create(contact); return new ModelAndView( "default" ).addObject(contact); } } Contact contact = new Contact(29L, "Doe" , "John" ); restTemplate .put( "http://localhost:8080/contact/services/contacts/" ,contact); Opération HTTP PUT HttpMessageConverter utilisé coté client Opération HTTP PUT HttpMessageConverter utilisé coté serveur (@RequestBody)
79.
80. @RequestBody Ex. d'utilisation du support REST sur le blog de Zenika : http://blog.zenika.com/index.php?post/2009/12/23/REST-avec-Spring-3.0-et-Solr @Controller public class ContactController { @RequestMapping (value= "/contactsl/{id}" ,method=RequestMethod. GET ) @ResponseBody public Contact get( @PathVariable Long id) throws Exception { return contactService .get(id); } }