1. Preface
In my spring Trip (a), we simply return a Web page or a JSON with a request without a reference, in the actual B/S, c/S network interaction, the request needs to define its own parameters. This article will simply add a number of references to the previous request.
2, the reference description ①method:api name, used to differentiate the service-side call method ②name: The request for the name, will be as the method of the actual 3, rewrite Hellocontroller.java
Package Com.nextgame.web;import Java.io.ioexception;import Net.sf.json.*;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import javax.servlet.http.*; @Controllerpublic class Hellocontroller {@RequestMapping ("/view") public String Hello (@RequestParam (value= "Hello", Required=false, Defaultvalue= "World") String name, model model) {Model.addattribute ("Hello", name); return "HelloWorld";} @RequestMapping ("/json") public void json (HttpServletRequest req,httpservletresponse res,string method,string name) Throws IOException {Jsonobject obj = new Jsonobject (), if (Method.equals ("SayHello")) {obj = This.sayhello (name);} else if (method.equals ("Sayhi")) {obj = This.sayhi (name);} Obj.put ("Copyright", "Wang Hao"); Res.getwriter (). print (obj); }/* * Api:sayhello */private Jsonobject SayHello (String name) {Jsonobject obj = new Jsonobject (); Obj.put ("Msg "," Hello, "+ name +"! "); return obj; }/* * Api:sayhi */private Jsonobject Sayhi (String name) {Jsonobject obj = new Jsonobject (); Obj.put ("msg", "Hi," + NA Me + "!"); return obj; }}
4. Run As Server
5. Use JSON pass-through to change the name's parameter type to JSON for defining the communication protocol yourself.
6. Service-side parsing JSON
public void json (HttpServletRequest req,httpservletresponse res,string method,jsonobject name) throws IOException { Jsonobject obj = new Jsonobject (), if (Method.equals ("SayHello")) {obj = This.sayhello (name.getstring ("name"));} else if (method.equals ("Sayhi")) {obj = This.sayhi (name.getstring ("name"));} Obj.put ("Copyright", "Wang Hao"); Res.getwriter (). print (obj);
7. Implementation
(--Naïve client program Ape!!! )
My spring Trip (ii): Adding a number of references to a request