java writes interfaces for mobile terminals

As a back-end language, Java is powerful in the web. Everyone knows all kinds of network applications that can be done by java. So in this era of mobile priority, how to continue to play the powerful role of java? Java is usually used as an app server to provide data and business logic for the app client, so we use java to write the interface, and the app client access interface returns json file for parsing, and finally realizes business logic.

In this way, we usually call it restful.

Restful is an architecture idea. It is a doctoral dissertation published by a doctoral student N years ago. Its core idea is front-end separation. The front-end accesses the back-end interface through http requests, such as www.xxxx.com/demo/username/password. Then the back-end encapsulates the processed data into JSON returns, so that the back-end only needs to focus on the specific logic to provide the interface, while the front-end only needs to provide the interface. Only care about the interface, improve the program decoupling. In the era of mobile priority, restful is extremely important. Usually a set of background can allow access to a variety of terminals, including mobile terminals and pc terminals. The Spring mvc framework, which is easier to implement restful in java, provides a set of annotations dealing with json. JSON data is returned by @ResponseBody, and JSON is parsed by @ResquestBody.

 

 

The following is a demo that ios visited my java background, which uses spring MVC and Hibernate.

//java terminal

 1 package cotroller;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 import java.util.List;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import jdk.nashorn.api.scripting.JSObject;
10 import model.Student;
11 import model.Teacher;
12 
13 import org.springframework.stereotype.Controller;
14 import org.springframework.ui.Model;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.RequestBody;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.ResponseBody;
20 
21 
22 
23 import dao.Get;
24 import dao.StudentDAO;
25 
26 //Land servlet
27 @Controller
28 public class LoginCotroller {    
29     /**
30      * 1. value="/doLogin/{username}/{password}" Intercepting xxx/doLogin/xx/xx
31      * 2. @ResponseBody Use this annotation to encapsulate the returned data type into json
32      * 3. @PathVariable("username") Intercept the value of {username} in request 1.value
33      * 4. Map<String, Object> The server puts the value into the map and encapsulates it as json. The client can easily get the value out by key.
34      */
35     
36     StudentDAO studentDAO = new StudentDAO();//Call the landing judgment method
37     
38     @RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
39     @ResponseBody
40     public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){    
41         System.out.println("Intercepted the client json request");
42         Map<String, Object> map = new HashMap<String, Object>();
43         
44         if(studentDAO.loginByStudent(username, password)){
45             System.out.println("Password correct");
46             map.put("result", "1");
47             return map; //Encapsulated as json Return to client
48         }
49             
50         System.out.println("Password error");
51         map.put("result", "0");
52         return map; //Encapsulated as json Return to client
53     }
54 
55 }


//ios terminal

 1 #import <Foundation/Foundation.h>
 2 #import <stdio.h>
 3 
 4 int main(int argc, const char * argv[]) {
 5     @autoreleasepool {
 6    
 7         char oldUsername[128];
 8         char oldPassword[128];
 9         
10         NSLog(@"Please enter a user name:");
11         scanf("%s", oldUsername);
12         NSString *username = [NSString stringWithUTF8String:oldUsername]; //Convert to NSString *
13         NSLog(@"Please input a password:");
14         scanf("%s", oldPassword);
15         NSString *password = [NSString stringWithUTF8String:oldPassword]; //Convert to NSString *
16         
17         //Visit springMVC Background and parse the returned json data
18         //Define an exception
19         NSError *error;
20         
21         //Definition request action Use stringWithFormat Stitching strings
22         NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
23         
24         //Load one NSURL object
25         NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
26         
27         //To send a request. url Data placement NSData In object
28         NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
29         
30         //NSJSONSerialization from response Resolve the data in the request and put it in the dictionary
31         NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
32         
33         NSString *resultValue = [jsonResult objectForKey:@"result"];
34         
35         NSLog(@"Your url yes%@", url);
36         NSLog(@"Server-side return value%@", resultValue);
37         
38         // oc String comparison method resultValue isEqualToString:@"1"] and java Of equlse Similar
39         if([resultValue isEqualToString:@"1"]){
40             NSLog(@"Login successfully!");
41         }else{
42             NSLog(@"Login failed,ERROR Incorrect username or password!");
43         }
44         
45         
46     }
47     return 0;
48 }

 

 

 



  

Tags: Java JSON Mobile Spring

Posted on Tue, 02 Apr 2019 01:06:29 -0400 by shana