preface

There are two common forms of interactive use in Spring Boot development projects: one is where the back-end interface returns directly to the page, and another is where it returns JSON data. The former is more common in traditional IT companies, with a template engine, returned by back-end page rendering. The latter is more commonly used by Internet companies, such as microservice interface, front and back end separation project, mobile app, etc., which are basically through JSON data interaction. The use of the back-end interface to return directly to the page is now less common, and JSON data interaction is still the dominant use. So today we’re going to do the first step in the back-end separation project, writing our first JSON interface.

Develop your first JSON interface

Start by creating the Spring Boot project, creating the Controller package and the VideoController.java file.

package com.gw.demo.controller;

import java.util.HashMap;
import java.util.Map;

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

@RestController
@RequestMapping("/api/v1/video")
public class VideoController {

    @RequestMapping("list")
    public Object list(a){

        Map<String,String > map = new HashMap<>();
        map.put("1"."SpringBoot course");
        map.put("2"."SpringCloud Micro Service Course");
        
        returnmap; }}Copy the code

In the Google browser input address: http://localhost:8080/api/v1/video/list can see browser display page is as follows:

PostMan is a necessary tool for front – end separation test

Of course, we can’t always write interfaces in the browser test, we need to use professional interface testing tools to test data. Next UP is the PostMan tool, which simulates browser requests, assembles parameters, and formats JSON response results. Installation address: www.getpostman.com/. Use PostMan to test the following:

Common annotations for SpringBoot

When I was developing my first JSON interface, I didn’t know what @SpringBootApplication, @RestController, @RequestMapping meant. Next I introduce a few of our usual development projects commonly used annotations, these annotations for the development of a small demo enough to use, of course, There are many commonly used annotations SpringBoot, we just briefly introduce a few here, interested students can baidu to search the relevant information.

1.@SpringBootApplication: This annotation is the cornerstone of a SpringBoot project and is added to the main class by default when it is created.

package com.gw.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

We can think of @SpringBootApplication as a collection of @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations. According to the Spring Boot official website, the three annotations are as follows: @enableAutoConfiguration: Enables the automatic configuration mechanism of Spring Boot. @ComponentScan: Scans beans annotated by @Component(@Service,@Controller). By default, annotations scan all classes in the package in which that class belongs. @Configuration: Allows the Spring context to register additional beans or import additional Configuration classes.

2.@Autowired: Automatically import objects into classes that are managed by the Spring container.

3.@Component: Generic annotations that annotate any class as a Spring component. If a Bean does not know which layer it belongs to, it can be annotated using the @Component annotation.

4.@Repository: corresponds to the persistence layer, namely Dao layer, which is mainly used for database-related operations.

5.@Service: corresponds to the service layer, which involves some complex logic.

6.@Controller: corresponds to the Spring MVC control layer, which mainly returns the page.

The 6.@RestController: @RestController annotation is a combination of @Controller and @responseBody and is primarily used to return JSON data or XML data.

7.@Configuration: Normally used to declare Configuration classes. You can use the @Component annotation instead, but using the @Configuration annotation to declare Configuration classes is more semantic.

I’ll cover the rest of the annotations in a later article, but familiarity with them should be enough to help you develop a small demo.

conclusion

That’s all for today’s summary, welcome to correct mistakes and add content!