1 | < user > |
2 | < firstname >Mary firstname > |
3 | < lastname >Smith lastname > |
4 | < preferences >nice preferences > |
5 | < preferences >pretty preferences > |
6 | < preferences >code preferences > |
7 | < username >marys username > |
8 |
user > |
Quote from Spring blog:
DELETE delete(String, String...)
GET getForObject(String, Class, String...)
HEAD headForHeaders(String, String...)
OPTIONS optionsForAllow(String, String...)
POST postForLocation(String, Object, String...)
PUT put(String, Object, String...)
This example only uses getForObject, postForObject, put and delete for the moment, so just the normal CRUD operations.
01 | package javaitzen.spring.rest.client; |
02 |
03 | import javaitzen.spring.rest.User; |
04 |
05 | import org.springframework.beans.factory.annotation.Autowired; |
06 | import org.springframework.stereotype.Component; |
07 | import org.springframework.web.client.RestClientException; |
08 | import org.springframework.web.client.RestTemplate; |
09 |
10 | @Component ( "userClient" ) |
11 | public class UserServiceClient { |
12 |
13 | @Autowired |
14 | protected RestTemplate restTemplate; |
15 | private final static String serviceUrl = "http://127.0.0.1:7001/Spring3RESTFul-1/app/users/" ; |
16 |
17 | public User retrieveUserDetails( final String firstName) { |
18 | System.out.println( "Client: get" ); |
19 | return restTemplate.getForObject(serviceUrl + "{firstName}" , User. class , firstName); |
20 | } |
21 | |
22 |
23 | public User createNewUser( final String firstName, final String lastName, final String userName) { |
24 | System.out.println( "Client: post" ); |
25 | User newUser = new User(firstName, lastName, userName); |
26 | newUser = restTemplate.postForObject(serviceUrl, newUser, User. class ); |
27 | return newUser; |
28 | } |
29 | |
30 | public void updateUserDetails( final String firstName, final String lastName, final String userName) { |
31 | System.out.println( "Client: put" ); |
32 | User newUser = new User(firstName, lastName, userName); |
33 | restTemplate.put(serviceUrl + "{firstName}" , newUser, firstName); |
34 | } |
35 |
36 | public boolean deleteUserDetails( final String firstName) { |
37 | System.out.println( "Client: delete" ); |
38 | try { |
39 | restTemplate.delete(serviceUrl + "{firstName}" , firstName); |
40 | } catch (RestClientException e) { |
41 | e.printStackTrace(); |
42 | return false ; |
43 | } |
44 | return true ; |
45 | } |
46 |
47 | |
48 | |
49 | } |
No comments:
Post a Comment