My spring Trip (ii): Adding a number of references to a request

Source: Internet
Author: User

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

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.