Install RESTClient from WizTools.org
Step 1: RESTClient is required to test the RESful web services. Go to
http://download.wiztools.org/rest-client/archive/index.html
Download the latest version of “restclient-ui-3.2.2-jar-with-dependencies.jar” file. Copy it to “c:\tools” and run it by Double-Clicking if you are running on windows. If outside windows you can run it as shown below.
1 2 | c:\tools>java -jar restclient-ui-3.2.2-jar-with-dependencies.jar |
pom.xml with XML & JSON data binding
Step 2: The pom.xml with “jackson-databind“, which will be used to convert the response data into JSON string, and “jackson-dataformat-xml“, which will be used to convert the response data to XML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simpleWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simpleWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- JSON data binding --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.9</version> </dependency> <!-- XML data binding --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.8.9</version> </dependency> </dependencies> <build> <finalName>simpleWeb</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> |
Define the model class Account.java
Step 3: Define the Account.java with 3 fields & getters/setters in the package “com.mytutorial.model”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.mytutorial.model; import java.io.Serializable; import java.math.BigDecimal; public class Account implements Serializable { private static final long serialVersionUID = 1L; private String id; private String name; private BigDecimal balance; public Account(String id, String name, BigDecimal balance) { this.id = id; this.name = name; this.balance = balance; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } @Override public String toString() { return "Account [id=" + id + ", name=" + name + ", balance=" + balance + "]"; } } |
Define the controller class AccountController.java
Step 3: Define the AccountController.java with 3 fields & getters/setters in the package “com.mytutorial.controller”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.mytutorial.controller; import java.math.BigDecimal; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.mytutorial.model.Account; @RestController @RequestMapping("/v1/forecasting") public class AccountController { @RequestMapping(value = "/accounts", method = RequestMethod.GET) public Account[] getAccounts() { Account[] accounts = new Account[] { new Account("123", "John R", BigDecimal.valueOf(235.00)), new Account("345", "Peter J", BigDecimal.valueOf(2505.60)) }; return accounts; } } |
Spring Java Configs for wiring up
Step 4: Spring Java configs in the package “com.mytutorial.config”
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.mytutorial.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.mytutorial.controller") public class SimpleWebConfiguration { } |
Bootstrap the Spring dispatcher Servlet with the mapping URI of “/entry/*” & the above “SimpleWebConfiguration” for the controllers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.mytutorial.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SimpleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SimpleWebConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] { "/entry/*" }; } } |
Build simpleWeb.war & deploy to Tomcat
Step 5: Copy the “simpleWeb.war” to Tomcat’s “webapps” folder and start the Tomcat server.
1 2 | c:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\bin>startup.bat |
Start the RESTClient from WizTools.org
Step 6: Double-Clicking “c:\tools\restclient-ui-3.2.2-jar-with-dependencies.jar” on windows or
1 2 | c:\tools>java -jar restclient-ui-3.2.2-jar-with-dependencies.jar |
XML Data
http://localhost:8080/simpleWeb/entry/v1/forecasting/account http://localhost:8080/simpleWeb/entry/v1/forecasting/accounts.json You need to add “.json” suffix to the URL to get JSON data. Q. What if you want the URL “http://localhost:8080/simpleWeb/entry/v1/forecasting/accounts” to return JSON data instead of XML. If you want it to return XML data: produces={MediaType.APPLICATION_XML_VALUE} Note that @RestController is used instead of “@Controller“. If you were to use “@Controller“, you need to use the annotation @ResponseBody as in “public @ResponseBody Account[] getAccounts()” as shown below:JSON Data
Further discussions
A. If you want it to return JSON data: “produces={MediaType.APPLICATION_JSON_VALUE}”:You may like