Springmvc rest-style URL request

Source: Internet
Author: User

Rest is an architectural style that uses URLs to access any resource on the network. One of the thoughts of rest is to use the action Get,post,put,delete in HTTP to do the additional pruning and checking.

This is the rest request for SPRINGMVC. Examples of jax-rs that do not contain webservice. The rest-style webservice can be implemented with the CXF framework. Also very simple.

1 First prepare the jar package required for the Web project, which is the jar that SPRINGMVC depends on:

2 Create a Dynamic Web project: Here first the CONFIG. xml file is registered SPRINGMVC's front controller, Dispatcherservlet, and all client requests are forwarded by him. Then configure Hiddenhttpmethodfilter to turn the POST request into put and delete

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0"><!--Hiddenhttpmethodfilter can turn the POST request into a put delete--<Filter>        <Filter-name>Hiddenhttpmethodfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class>    </Filter>    <filter-mapping>        <Filter-name>Hiddenhttpmethodfilter</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>                <!--Configure the SPRINGMVC dispatcherservlet distribution request, in fact he is a front-end controller -    <servlet>        <Servlet-name>Dispatcherservlet</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Dispatcherservlet</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping></Web-app>

3 Writing the rest-style interface

 PackageCn.bean.demo.service;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller @requestmapping (Value= "/restservice") Public classRestservice { Public Final StaticString succeedd= "Show"; /*** GET request * URL:http://localhost: 8080/SPRINGMVC/RESTSERVICE/TESTRESTGET/12 *@paramID * Parameters for query *@return     */@RequestMapping (Value= "/testrestget/{id}", method=requestmethod.get) PublicString Testrestget (@PathVariable ("id") (Integer ID) {SYSTEM.OUT.PRINTLN ("Rest-style GET request ... id=." +ID); returnSucceedd; }    /*** Post new * URL:http://localhost: 8080/springmvc/restservice/testrestpost *@return     */@RequestMapping (Value= "/testrestpost", method=requestmethod.post) PublicString Testrestpost () {System.out.println ("Rest-style POST request .... "); returnSucceedd; }    /*** PUT Modify action * URL:http://localhost: 8080/springmvc/restservice/testrestput/put123 *@paramname *@return     */@RequestMapping (Value= "/testrestput/{name}", method=requestmethod.put) PublicString Testrestput (@PathVariable ("name") (String name) {System.out.println ("Rest-style put request ..." name= "+name); returnSucceedd; }    /*** Delete delete operation * URL:http://localhost: 8080/SPRINGMVC/RESTSERVICE/TESTRESTDELETE/11 *@paramID *@return     */@RequestMapping (Value= "/testrestdelete/{id}", method=requestmethod.delete) PublicString testrestdelete (@PathVariable Integer id) {SYSTEM.OUT.PRINTLN ("Rest-style Delete request ... id=." +ID); returnSucceedd; }            }

4 writing the interface's response page-The return of the interface

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body>   <H2>Show this is Succeedd? Yes</H2></Body></HTML>

5 Publishing and configuring the Rest interface Dispatcherservlet-servlet.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd ">    <!--Configuring a custom scanned package -    <Context:component-scanBase-package= "Cn.bean.demo"></Context:component-scan>    <!--Configure the View resolver: How to resolve the handler method return value to the actual physical view -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/views/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean></Beans>

6 Testing the Rest interface: Publish the project to TOMCAT7. Test Tool Restclient

Springmvc rest-style URL request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.