Spring handles request and response related annotations

@Controller

By default, the content of the string.html page in the templates directory is returned.
Add @ ResponseBody annotation to the method to return the content of JSON, XML or custom mediaType

@RestController

Returning the content directly will automatically convert the object entity to JSON format, and the view parser, InternalResourceViewResolver, will not work.
@RestController = @Controller + @ResponseBody

@RequestBody

The JSON data in the request body is received and assigned to the property through the setter method of the entity class.
json's' = > entity String is' '
'' = > entity Integer and Double of json are null
json's null = > entity is null
@RequestBody can be used at the same time with @ RequestParam(), there can be at most one @ RequestBody and multiple @ RequestParam().

Receive data in String

@RequestMapping("/index")
public String indexMapping(@RequestBody String jsonStr) {
    return jsonStr;
}

Receive data as object entity

// {"name":"hanmeimei","age":12}
@RequestMapping("/index")
public String indexMapping(@RequestBody User user) {
    return user.toString();
}

Receive data as a complex object entity

public class Team {
    private Integer id;
    private String name;
    private List<String> honors;
    private List<User> members;
}

// {
//     "id": 1,
//     "name": "good",
//     "honors": ["very good", "very fast"],
//     "members": [{"name":"hanmeimei","age":12},
//                 {"name":"lilei","age":13}],
// }
@RequestMapping("/index")
public String indexMapping(@RequestBody Team team) {
    return team.toString();
}

@ResponseBody

Convert the object entity to JSON, XML, or custom mediaType content and return it in HTTP response body

@RequestMapping

Mapping requests to controllers can be used on controller classes and / or methods.

Processing a single request

@RequestMapping("/home")
public class IndexController {
    @RequestMapping("/index")
    String indexMapping() {
        return "Hello from index mapping.";
    }
}

Processing multiple requests

@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = {
        "/",
        "/index",
        "/index/*.html",
        "/index/**/*.html"
    })
    String indexMultipleMapping() {
        return "Hello from index multiple mapping.";
    }
}

Processing request type

The default is HTTP GET type.

@RequestMapping(value = "/home", method = RequestMethod.GET)
String get() {}

@RequestMapping(value = "/home", method = RequestMethod.DELETE)
String delete() {}

@RequestMapping(value = "/home", method = RequestMethod.POST)
String post() {}

@RequestMapping(value = "/home", method = RequestMethod.PUT)
String put() {}

@RequestMapping(value = "/home", method = RequestMethod.PATCH)
String patch() {}

Process request type shortcut

@GetMapping(value = "/home")
String get() {}

@DeleteMapping(value = "/home")
String delete() {}

@PostMapping(value = "/home")
String post() {}

@PutMapping(value = "/home")
String put() {}

@PatchMapping(value = "/home")
String patch() {}

Dealing with objects of production and consumption

public class IndexController {
    // Production application/JSON response
    @RequestMapping(value = "/prod", produces = {
        "application/JSON"
    })
    @ResponseBody
    String getProduces() {
        return "Produces attribute";
    }

    // Consuming application / JSON & Application / XML requests
    @RequestMapping(value = "/cons", consumes = {
        "application/JSON",
        "application/XML"
    })
    @ResponseBody
    String getConsumes() {
        return "Consumes attribute";
    }
}

Processing headers

public class IndexController {
    // Processing requests with content type = Application / JSON
    @RequestMapping(value = "/head", headers = {
        "Content-Type=application/json"
    })
    String head() {
        return "Mapping applied along with headers";
    }
}
public class IndexController {
    @RequestMapping(value = "/head", headers = {
        "Content-Type=text/plain",
        "Content-Type=application/json"
    })
    String head() {
        return "Mapping applied along with headers";
    }
}

Processing request parameters

public class IndexController {
    @RequestMapping(value = "/fetch", params = {
        "personId=10"
    })
    String getParams10(@RequestParam("personId") String id) {
        return "Fetched parameter using params attribute = " + id;
    }

    @RequestMapping(value = "/fetch", params = {
        "personId=20"
    })
    String getParams20(@RequestParam("personId") String id) {
        return "Fetched parameter using params attribute = " + id;
    }
}

Processing dynamic URI s

public class IndexController {
    @RequestMapping(value = "/fetch/{id}")
    String getDynamicUriValue(@PathVariable String id) {
        return "Dynamic URI parameter fetched";
    }

    @RequestMapping(value = "/fetch/{id:\d+}/{name}")
    String getDynamicUriValueRegex(
        @PathVariable("id") int id, @PathVariable("name") String name
    ) {
        return "Dynamic URI parameter fetched using regex";
    }
}

Default processing method

public class IndexController {
    @RequestMapping()
    String default () {
        return "This is a default method for the class";
    }
}

Reference resources Use of RequestBody

Tags: Java JSON xml Attribute

Posted on Fri, 27 Mar 2020 07:26:11 -0700 by hoodlumpr