Please ask me some questions when I call the API on Spring.

Asked 2 years ago, Updated 2 years ago, 41 views

There are server A and server B, and the network is different from each other, and the DB link cannot be connected due to security issues.

So I have to get the data from server A through API Since server A is an API provider, isn't it right to make four CRUD stores and provide them? (Read (R) is get, and the rest is created, corrected, and deleted (CUD) is post)

By the way, should I make 4 B servers to call the API and manage them?

Now, I'm going to call one by get and one by post, and I'm going to make two in a common class Each controller makes a call (get or post) as necessary.

@Controller
@RequestMapping("/user")
public class UserController {

CaController caController = new CaController();

...
//When importing data
@RequestMapping(value="getUserInfo.do")
public ModelAndView getUserInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam Map<String, Object> data) throws Exception {
...
//get method
String result = caController.getMethod(data);
...
}

...
//Delete data (create, modify, delete same..) deleteUserInfo, updateUserInfo, createUserInfo)
@RequestMapping(value="deleteUserInfo.do")
public ModelAndView deleteUserInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam Map<String, Object> data) throws Exception {
...
//post method
String result = caController.postMethod(data);
...
}

I wonder if this is the right way.

1) Functions used in the get method (when importing data)

public String getMethod (@RequestParamMap<String, Object>data) throwsException {
    URL url = new URL(data.url);
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
    StringBuffer sb = new StringBuffer();
    String tempStr = null;

    while(true) {
        tempStr = br.readLine();
        if(tempStr == null) break;
        sb.append(tempStr); 
    }
    br.close();
    return sb.toString();
}

2) Functions used in the post method (data generation, modification, and deletion)

public String postMethod (@RequestParamMap<String, Object>data) throwsException {
    String iStr = null;
    StringBuffer outBuf = new StringBuffer();
    String param = getJsonStringForMap(data);

    URL url = new URL(apiUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    try {
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept-Charset", "UTF-8");

        OutputStream os = conn.getOutputStream();
        os.write(param.getBytes("UTF-8"));
        os.flush();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        while((iStr = in.readLine()) != null) {
            outBuf.append(iStr);
        }
    } } catch(Exception e) {
        e.printStackTrace();
    } } finally {
        conn.disconnect();
    }

   return outBuf.toString();
}

Or I wonder if it's a different way to do it.

java spring

2022-09-21 11:32

1 Answers

Just because there are four APIs provided, it is not necessary to match the number of APIs used. As long as it meets the request format required by the API, the rest is entirely up to the client.

For example, server B needs only three methods: one inquiry method, one creation and modification method, and one deletion method. Of course, you only need two questions.

Rather than that, you can design it by considering how server B is actually used.


2022-09-21 11:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.