using-lishys-notes.notes
Guide
The guide pane on the right contains information organized into sections.
Use the

and

buttons to move through the sections, or click an accordion tab (shown below) to go directly to a particular topic.

Code Reference
Each paragraph in the guide is usually associated with a particular piece of code. An orange arrow indicates that the code displayed in the middle pane is relevant to the guide text.

If the arrow is gray, simply click anywhere on the paragraph and the relevant code will be displayed.

Code Highlighter
The highlight feature identifies areas of the code which relate to a word or term in the guide. Hover the mouse over any underlined word and the relevant code will be highlighted.
You can disable the highlighting feature at any time by clicking on the

button.
Don't forget to make the appropriate code page visible by clicking on any paragraph with a gray arrow.
BrowserRestController.java
package lishy.rest.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/browser")
public class BrowserRestController {
private int id = 100;
@RequestMapping(value="/resource/{id}", method=RequestMethod.GET)
public ModelAndView getRequest(@PathVariable("id") int resourceId) {
return new ModelAndView("rest", "message", "GET request for resource " + resourceId);
}
@RequestMapping(value="/parent/{parentId}/child/{childId}", method=RequestMethod.GET)
public ModelAndView getRequestWithParent(@PathVariable("parentId") int parentId,
@PathVariable("childId") int childId) {
return new ModelAndView("rest", "message", "GET request for " + parentId +
" and " + childId);
}
@RequestMapping(value="/resource", method=RequestMethod.GET)
public ModelAndView getRequestForMultipleResources() {
return new ModelAndView("rest", "message", "GET request for multiple resources (no id)");
}
@RequestMapping(value="/resource", method=RequestMethod.POST)
public ModelAndView postRequest(@RequestParam("formfield") String formField) {
id++;
return new ModelAndView("rest", "message", "POST created resource " + id +
" with " + formField);
}
@RequestMapping(value="/resource/{id}", method=RequestMethod.PUT)
public ModelAndView putRequest(@PathVariable("id") int resourceId,
@RequestParam("formfield") String formField) {
return new ModelAndView("rest", "message",
"PUT created or updated resource " + resourceId +
" with " + formField);
}
@RequestMapping(value="/resource/{id}", method=RequestMethod.DELETE)
public ModelAndView deleteRequest(@PathVariable("id") int resourceId) {
return new ModelAndView("rest", "message", "DELETE deleted resource " + resourceId);
}
}
RestServerController.java
package lishy.rest.web;
import lishy.rest.RestUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RestServerController {
private int id = 100;
@RequestMapping(value="/resource/{id}", method=RequestMethod.GET)
@ResponseBody public String getRequest(@PathVariable("id") int resourceId) {
return "Return representation of resource " + resourceId + " in web response body";
}
@RequestMapping(value="/parent/{parentId}/child/{childId}", method=RequestMethod.GET)
@ResponseBody public String getRequestWithParent(@PathVariable("parentId") int parentId,
@PathVariable("childId") int childId) {
return "Return representation of resource " + parentId + " and " + childId;
}
@RequestMapping(value="/resource", method=RequestMethod.POST)
public ResponseEntity<String> postRequest(@RequestBody String body) {
id++;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Location", "http://localhost:8080/rest/app/resource/" + id);
return new ResponseEntity<String>("Created resource " + id + " with " + body,
responseHeaders,
HttpStatus.CREATED);
}
@RequestMapping(value="/resource/{id}", method=RequestMethod.PUT)
public ResponseEntity<String> putRequest(@PathVariable("id") int resourceId,
HttpEntity<String> requestEntity) {
System.out.println("Create or update resource " + resourceId +
" with " + requestEntity.getBody());
RestUtils.showHttpEntityContents(requestEntity);
return new ResponseEntity<String>(" ", HttpStatus.OK);
}
@RequestMapping(value="/resource/{id}", method=RequestMethod.DELETE)
public ResponseEntity<String> deleteRequest(@PathVariable("id") int resourceId,
@RequestHeader("Host") String hostHeader) {
System.out.println("Delete resource " + resourceId);
System.out.println("Host: " + hostHeader);
return new ResponseEntity<String>(" ", HttpStatus.OK);
}
}
RestClient.java
package lishy.rest;
import java.net.URI;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestClient {
private static final String URL = "http://localhost:8080/rest/app/";
public static void main(String[] args) {
getRequest();
postRequest();
putRequest();
deleteRequest();
}
private static void getRequest() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> entity = restTemplate.getForEntity(URL + "resource/{id}",
String.class,
"123");
RestUtils.showHttpEntityContents(entity);
String response = restTemplate.getForObject(URL + "parent/{parentId}/child/{childId}",
String.class,
"123", "456");
System.out.println(response);
}
private static void postRequest() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> entity = restTemplate.postForEntity(URL + "resource",
"Representation of a new resource",
String.class);
RestUtils.showHttpEntityContents(entity);
String response = restTemplate.postForObject(URL + "resource",
"Representation of another new resource",
String.class);
System.out.println(response);
URI uri = restTemplate.postForLocation(URL + "resource",
"Representation of yet another new resource");
System.out.println(uri);
}
private static void putRequest() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.put(URL + "resource/{id}",
"Representation of a new or existing resource",
"123");
}
private static void deleteRequest() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete(URL + "resource/{id}", "123");
}
}
RestUtils.java
package lishy.rest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
public class RestUtils {
public static void showHttpEntityContents(HttpEntity<String> http) {
System.out.println("Headers..");
HttpHeaders headers = http.getHeaders();
for (String key: headers.keySet()) {
System.out.println(" " + key + ": " + headers.getFirst(key));
}
System.out.println("Body..");
System.out.println(" " + http.getBody());
}
}
rest.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
<h1>REST from the Browser</h1>
<div style="color:green">${message}</div>
<p>
<a href="/rest/app/browser/resource/321">GET request</a><br/>
<a href="/rest/app/browser/parent/123/child/456">GET request with parent</a><br/>
<a href="/rest/app/browser/resource">GET request for multiple resources</a>
</p>
<form:form action="/rest/app/browser/resource" method="POST">
<button type="submit">Post</button>
<input type="text" name="formfield" value="abc">
</form:form>
<form:form action="/rest/app/browser/resource/321" method="PUT" methodParam="httpMethod">
<button type="submit">Put</button>
<input type="text" name="formfield" value="xyz">
</form:form>
<form:form action="/rest/app/browser/resource/321" method="DELETE" methodParam="httpMethod">
<button type="submit">Delete</button> <i>with</i> Spring form tag
</form:form>
<form action="/rest/app/browser/resource/456" method="POST">
<button type="submit">Delete</button> <i>without</i> Spring form tag
<input type="hidden" name="httpMethod" value="DELETE">
</form>
</body>
</html>
rest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="lishy.rest.web"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>Rest</display-name>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hiddenMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
<init-param>
<param-name>methodParam</param-name>
<param-value>httpMethod</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hiddenMethodFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>app/browser/resource/321</welcome-file>
</welcome-file-list>
</web-app>