preface

As an iOS/ Flutter engineer, I recently had a temporary need to build a proposed server. However, I have never been involved in server and data development in my previous career, so… I’m kind of at a loss

But as a qualified engineer, always ready to point to where the fight where the preparation, no can not, so, Baidu to help get up ~

In actual combat

The following record, I set up the server, and then get the data stored in the local database of a series of operation steps, and encountered pits and solutions. This article is probably only suitable for and I like Java, database small white, but also encountered such a “challenge” partners look, if you are a server god, Java god, see this article, if written wrong, please do not hesitate to comment ~

Setting up the Suggestion Server

The preparatory work

  • Mac (which means you have to have a computer, haha, I just want to point out that I use a Mac, which may not be the same as Windows, so I’ll use the Mac as an example.)
  • IntelliJ IDEA
  • MySql

IntelliJ IDEA can be downloaded from the official website, but note that we need to download Ultimate, and this version is charged (cracked version, or cracked method, your own baidu), but we need to use this version to do server development, the free version does not support the function of Spring.

The next step is to install MySQL. I recommend using Homebrew to install MySQL. I think I have installed MySQL on my computer before, so I won’t go into details here

brew install mysql
Copy the code

Setting up the server

First, make sure you have a working IntelliJ IDEA. Here are some screenshots of each step

  1. Create an empty project

Fill in the project name

Default configuration next (this step may timeout error, just try again)

This is modified as needed

The following two steps are required

And then we’re more than halfway there

Then he needs to wait for a while, should be downloading a dependency package or something, there will be a progress bar in the lower right corner, so that he completed the download, then proceed to the next operation.

  1. Create a Controller

So let’s look at the following three annotations, if you’re interested, but let’s use the 🙄 @RestController flag so this is a controller

@responseBody will wrap and return the result

RequestMapping matches the foreground request path

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyTestController {

    @ResponseBody
    @RequestMapping("/test")
    public String test() {return "test spring boot!!"; }}Copy the code

Here is a shortcut key Option+Enter to automatically import or solve other problems, you can try ~

  1. Start the server

The test results

  1. Other annotations @requestMapping
package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyTestController {

    @ResponseBody
    @RequestMapping("/test")
    public String test() {return "test spring boot!!"; }}Copy the code

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class MyTestService {
    public String sayHello() {return "Hello this is Service"; }}Copy the code

Changes in Controller

package com.example.demo.controller;

import com.example.demo.service.MyTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyTestController {
    
    @Autowired
    MyTestService myTestService;

    @ResponseBody
    @RequestMapping("/test")
    public String test() {return "test spring boot!!";
    }


    @ResponseBody
    @RequestMapping("/testService")
    public String testService() {returnmyTestService.sayHello(); }}Copy the code

Here is the structure of the project so far

In addition, parameters are passed in the same way as methods in other languages, with some spring-specific annotation injection. Then their actual operation will understand

@ResponseBody
    @RequestMapping("/testParam1")
    public String testParam1(String param1){
        return "test param1 is " + param1;
    }

    @ResponseBody
    @RequestMapping("/testParam2")
    public String testParam2(String param1, @RequestParam(defaultValue = "30") int param2){
        //@RequestParam(defaultValue = "30") Sets the default value of param2return "test param1 is " + param1 + "param2 is " + param2;
    }

    @ResponseBody
    @RequestMapping("/testParam3/{param1}/{param2}")
    public String testParam3(@pathVariable String param1, @pathVariable int param2){// @pathvariable Sets path variablesreturn "test param1 is " + param1 + "param2 is " + param2;
    }
Copy the code

Test results:

The testParam3 parameter is directly/separated. Do not use it any more? Key =value&key=value

Today I will write here, later in the update request other services to get the data, and then local storage ~