Follow on Google News News By Tag Industry News News By Place Country(s) Industry News
Follow on Google News | How to use Spring RESTTemplate to post data to a web service by Johnathan Mark SmithJohnathan Mark Smith will show you how To Use Spring RESTTemplate To Post Data to a Web Service
By: StutteringTech.com Using Spring RESTTemplate to Post Objects to RESTful web services with Spring’s Java Configuration (JavaConfig) In this example I am going to show you how to post data to a RESTful web service in Java using Spring, Spring Java Configuration and more Web Service Code Let’s take a quick look at the Spring MVC Web Service code on the server: @Controller @RequestMapping("/ class JSonController { private static final Logger logger = LoggerFactory.getLogger( @RequestMapping( @ResponseBody public User updateCustomer(@ logger.debug(" logger.debug(" return new User("NEW123" }< As you can see from the code above the web service is goign to what for a ID and user object to be passed in and then its going to create a new User Object and send it back to the client. Lets take a quick look inside the User Object public class User { private String user; private String name; public User() { } public User(String user, String name) { this.user = user; this.name = name; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getName() { return name; } public void setName(String name) { this.name = name; } }< So you can see from the above code that the user object has to fields user and name. Time For The Client Code You can see from the client code below is that we are using Spring RESTTemaple and going to post an User Object to a web server and get one back. @PropertySource(" public class Main { /** * Setting up logger */ private static final Logger LOGGER = getLogger(Main.class); public static void main(String[] { LOGGER.debug(" /** * * This is going to setup the REST server configuration in the applicationContext * you can see that I am using the new Spring's Java Configuration style and not some OLD XML file * */ ApplicationContext context = new AnnotationConfigApplicationContext( /** * * We now get a RESTServer bean from the ApplicationContext which has all the data we need to * log into the REST service with. * */ RESTServer mRESTServer = context.getBean( /** * * Setting up data to be sent to REST service * */ Map<String, String> vars = new HashMap<String, String>(); vars.put("id" /** * * Doing the REST call and then displaying the data/user object * */ try { /* This is code to post and return a user object */ RestTemplate rt = new RestTemplate(); rt.getMessageConverters() rt.getMessageConverters() String uri = new String(mRESTServer.getHost() User u = new User(); u.setName("Johnathan M Smith"); u.setUser("JS01"); User returns = rt.postForObject( LOGGER.debug(" } catch (HttpClientErrorException e) { /** * * If we get a HTTP Exception display the error message */ LOGGER.error(" ObjectMapper mapper = new ObjectMapper(); ErrorHolder eh = mapper.readValue( LOGGER.error(" } catch(Exception e) { LOGGER.error(" } } }< You can see from the above code how easy it is to use RESTTeample to post data to a web service. We Can I Get The Source Code You can checkout the project from github. git clone git@github.com: cd springmvc-resttemplate- Here is the video: End
|
|