1. Create a project: File-"New-" Project-"Spring Initializr-"Next-"Next Load web-"Spring web; SQL-"Spring Data JPA, MySQLDriver; Template Engines-" Thymeleaf on the dependencies page;
2. After creation, create a controller package under src/main/java/{demo}, and add a java class file under it.
3. Add the following code to the newly added class:
package firsttest.firstspringtest.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAutoConfiguration
@RestController
public class HelloWorldController {
@RequestMapping("/helloworld")
public String helloworld()
{
return "Hello World";
}
}
4. Modify the main function to:
public static void main(String[] args) {
SpringApplication.run(HelloWorldController.class, args);
}
5. On the Maven tab on the right, expand "lifecycle", right-click on "package", select "Run Maven Build", and a jar will be generated under its compiled address.
6. Use java -jar XXXX.jar in cmd to use the service. Enter localhost:8080/helloworld in the browser to access the service. Note: Because there is @RequestMapping("/helloworld") in the controller java class file, /helloworld. If it is modified ("/{demo}"), it can be replaced.
7. The default port number is "8080", which can be modified through application.properties under the project "resources". As follows, modify the port to 8016
server.port=8016
8. Copy the executable jar under target to the server. Available (xshell + xftp tool https://download.csdn.net/download/tianyueWindbg/15535309 )
10. Execute java -jar XXXX.jar in xshell. (Sometimes it will report: Error: Unable to access jarfile firstspringtest-0.0.1-SNAPSHOT.jar, please switch to the directory where the jar package is located)
If JDK is not installed, you can refer to: https://blog.csdn.net/tianyueWindbg/article/details/114292990
11. Open the server port, enter the public IP: 8016/helloworld in the browser address of the local computer, and verify that it can be accessed, then the deployment is successful.