<repository>
<id>spring-milestone</id>
<name>Spring Portfolio Milestone Repository</name>
<url>http://s3.amazonaws.com/maven.springframework.org/milestone</url>
<!--
To browse repo use URL:
http://s3browse.com/explore/maven.springframework.org/milestone
-->
</repository>
File: web.xml
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<!-- attempt to get rid of error
ERROR [org.hibernate.LazyInitializationException] - <could not initialize proxy - the owning Session was closed>
-->
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SC_OK (200) SC_CREATED (201) SC_MOVED_PERMANENTLY (301) SC_BAD_REQUEST (400) Status code (400) indicating the request sent by the client was syntactically incorrect. SC_NOT_FOUND (404) SC_GONE (410) SC_UNSUPPORTED_MEDIA_TYPE (415) SC_UNAUTHORIZED (401) SC_REQUEST_ENTITY_TOO_LARGE (413) SC_NO_CONTENT (204) SC_METHOD_NOT_ALLOWED (405) SC_INTERNAL_SERVER_ERROR (500) SC_FORBIDDEN (403)
Also see List of HTTP status codes at Wikipedia
http://localhost:8080/restschooldemo/spring/rest/school/v1/student/2
http://localhost:8080/restschooldemo/spring/rest/school/v1/student
http://localhost:8080/restschooldemo/spring/rest/school/v1/student?fn=J&ln=
http://localhost:8080/restschooldemo/spring/rest/school/v1/student?fn=Jjennifer&ln=Vendama
File: XStreamCustomFactoryImpl.java
//---------------------------------------------------------
public static XStream getXStreamInstance()
{
XStream xstream = new XStream() {
protected MapperWrapper wrapMapper(MapperWrapper next)
{
return new HibernateMapper(next);
}
};
xstream.registerConverter(
new HibernateProxyConverter(xstream.getMapper(),
new PureJavaReflectionProvider()),
XStream.PRIORITY_VERY_HIGH);
return xstream;
}
...
//---------------------------------------------------------
public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response)
{
// do something useful here
ModelAndView mav = new ModelAndView(xmlView);
return mav;
}
//---------------------------------------------------------
public ModelAndView doPost(HttpServletRequest request, HttpServletResponse response)
{
// do something useful here
ModelAndView mav = new ModelAndView(xmlView);
return mav;
}
//---------------------------------------------------------
public ModelAndView doPut(HttpServletRequest request, HttpServletResponse response)
{
// do something useful here
ModelAndView mav = new ModelAndView(xmlView);
return mav;
}
//---------------------------------------------------------
public ModelAndView doDelete(HttpServletRequest request, HttpServletResponse response)
{
// do something useful here
ModelAndView mav = new ModelAndView(xmlView);
return mav;
}
...
File: com.nbtconsulting.demo.restws.v2007.web.BasicSpringRestController
...
//---------------------------------------------------------
/**
Primary "do work" method called when a request is made
on the web service.
*/
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)
{
logger.debug("entered");
logger.debug("supportedMethods: ${this.supportedMethods}");
ModelAndView mav = null;
String strRequestHttpMethod = request.getMethod();
logger.debug("strRequestHttpMethod: " + strRequestHttpMethod);
if (METHOD_GET.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doGet(request, response);
}
else if (METHOD_PUT.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doPut(request, response);
}
else if (METHOD_POST.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doPost(request, response);
}
else if (METHOD_DELETE.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doDelete(request, response);
}
else if (METHOD_HEAD.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doHead(request, response);
}
else if (METHOD_OPTIONS.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doOptions(request, response);
}
else if (METHOD_TRACE.equalsIgnoreCase(strRequestHttpMethod))
{
mav = doTrace(request, response);
}
else
{
//default:
//mav = doNothing(request, response);
}
return mav;
}
...
File: com.nbtconsulting.demo.restws.v2007.web.BasicSpringRestController
public class BasicSpringRestController
extends AbstractController
{
// following METHOD_* constants are not currently provided by Spring Framework;
// however, METHOD_GET and METHOD_POST are provided by Spring Framework
public static final String METHOD_PUT = "PUT";
public static final String METHOD_DELETE = "DELETE";
public static final String METHOD_HEAD = "HEAD";
public static final String METHOD_OPTIONS = "OPTIONS";
public static final String METHOD_TRACE = "TRACE";
...
//---------------------------------------------------------
public BasicSpringRestController()
{
super();
String[] strarraySupportedHttpMethods =
{
METHOD_GET,
METHOD_POST,
METHOD_PUT,
METHOD_DELETE,
//METHOD_HEAD,
//METHOD_OPTIONS,
//METHOD_TRACE,
};
setSupportedMethods(strarraySupportedHttpMethods);
}
...
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="2"/>
<property name="mappings">
<props>
<prop key="/welcome.htm">clinicController</prop>
<prop key="/vets.htm">clinicController</prop>
...
<bean id="urlMappingRest"
class="carbonfive.spring.web.pathparameter.ParameterizedUrlHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<props>
<prop key="/spring/rest/school/v1/school">schoolRestController</prop>
...
File: MySpringWebApp-servlet.xml
<!--
This URL mapping can pull parameters off of the URL path
and make them available to the controller.
Thus it enables a feature of REST web service.
-->
<bean id="urlMappingRest"
class="carbonfive.spring.web.pathparameter.ParameterizedUrlHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<props>
<prop key="/spring/rest/school/v1/school">schoolRestController</prop>
<prop key="/spring/rest/school/v1/school/(*:id)">schoolRestController</prop>
<prop key="/spring/rest/school/v1/student">studentRestController</prop>
<prop key="/spring/rest/school/v1/student/(*:id)">studentRestController</prop>
...
File: com.nbtconsulting.demo.restws.v2007.web.BasicSpringRestController
...
//---------------------------------------------------------
/**
If present, get the "id" paramater from the URL path.
@return null if id parameter is not present on the URL path,
else returns the id value from the URL path.
*/
public String getIdFromUrlIfPresent(HttpServletRequest request)
{
String strIdReturn = null;
Map mapRestParams = (Map) request.getAttribute(
"ParameterizedUrlHandlerMapping.path-parameters");
if (mapRestParams == null)
{
return null;
}
strIdReturn = (String) mapRestParams.get("id");
return strIdReturn;
}
...
...
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
...
public class SchoolRepositoryRestImpl
implements
ISchoolRepository,
IWebServiceConfigClient
{
protected static final String strUrlRootResource = "school";
protected WebServiceConfig webServiceConfig;
protected HttpClient httpClient = new HttpClient();
...
//---------------------------------------------------------
public void update(School objData) throws Exception
{
logger.debug("entered");
try
{
String strId = objData.getId().toString();
logger.debug("strId: " + strId);
String strRestWebServiceUrl = buildWebServiceUrlForTargetItem(strId);
PutMethod method = new PutMethod(strRestWebServiceUrl);
XStream xstream = XStreamCustomFactoryImpl.getXStreamInstance();
String xmlStr = xstream.toXML(objData);
logger.debug("xmlStr: " + xmlStr);
RequestEntity requestEntity = new StringRequestEntity(xmlStr, null, null);
method.setRequestEntity(requestEntity);
httpClient.executeMethod(method);
}
catch (HttpException ex)
{
logger.error(
"Exception during accessing REST web service to update the data item.",
ex);
throw ex;
}
catch (IOException ex)
{
logger.error(
"Exception during accessing REST web service to update the data item.",
ex);
throw ex;
}
}