SPRINGMVC support for adding mock JSON

Source: Internet
Author: User

Add the mock functionality returned after parsing the JSON file under Server Classpath in SPRINGMVC:

Import Java.io.filenotfoundexception;import java.io.ioexception;import java.io.inputstream;import Java.net.URL; Import Java.net.urlconnection;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;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.restcontroller;import Com.fasterxml.jackson.databind.jsonnode;import Com.fasterxml.jackson.databind.ObjectMapper, @RestController @requestmapping ("/mock") public class Mockcontroller {@ Requestmapping (value = "/{module}/{action}", method = requestmethod.post) public jsonnode mockcontent (@PathVariable Str ing module, @PathVariable String action, httpservletrequest request, httpservletresponse response) {Jso        Nnode actualobj = null;        InputStream FIS = null; try {ClassLoader Ctxloader = ThreaD.currentthread (). Getcontextclassloader ();            URL Resurl = Ctxloader.getresource (action + ". JSON");            URLConnection resconn = Resurl.openconnection ();            Resconn.setusecaches (FALSE);            FIS = Resconn.getinputstream ();            StringBuilder builder = new StringBuilder ();            int ch;            while ((ch = fis.read ())! =-1) {builder.append ((char) ch);            } objectmapper mapper = new Objectmapper ();        Actualobj = Mapper.readtree (builder.tostring ());        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();                } finally {if (FIS! = null) {try {fis.close ();    } catch (IOException e) {}}} return actualobj; }}

  

Other types of support can also be added, such as string data, or something else. Since the Springmvc rest interface uses the JSON converter by default, it needs to be converted to the corresponding JSON format

SPRINGMVC support for adding mock JSON

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.