@Controller: Modifier class, used to create an object that handles HTTP requests
@RestController: Spring4 added after the note, originally returned in @controller JSON needs @responsebody to cooperate, If you replace @controller directly with @restcontroller, you do not need to configure the @ResponseBody, which returns the JSON format by default.
@RequestMapping: Configuring URL Mappings
Below we try to use spring MVC to implement a restful API for manipulating user objects, with comments detailing how HTTP requests are mapped in spring MVC, and how to pass parameters.
The RESTful API is specifically designed as follows:
The Direct Stick Code:
1 PackageCom.yucong.yucongdemo.controller;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 ImportJava.util.HashMap;6 Importjava.util.List;7 ImportJava.util.Map;8 9 ImportOrg.springframework.web.bind.annotation.ModelAttribute;Ten Importorg.springframework.web.bind.annotation.PathVariable; One Importorg.springframework.web.bind.annotation.RequestMapping; A ImportOrg.springframework.web.bind.annotation.RequestMethod; - ImportOrg.springframework.web.bind.annotation.RestController; - the ImportCom.yucong.yucongdemo.domain.User; - - @RestController -@RequestMapping (value= "/users")//with this configuration, the following mappings are under/users + Public classUsercontroller { - + //Create a thread-safe map A StaticMap<long, user> users = Collections.synchronizedmap (NewHashmap<long, user>()); at -@RequestMapping (value= "/", method=requestmethod.get) - PublicList<user>getuserlist () { - //GET request to handle "/users/" to get the list of users - //You can also use @requestparam to pass parameters from the page to query conditions or to transfer page information. -list<user> r =NewArraylist<user>(Users.values ()); in returnR; - } to +@RequestMapping (value= "/", method=requestmethod.post) - PublicString postuser (@ModelAttribute user user) { the //a POST request that handles "/users/" to create a user * //In addition to the @modelattribute binding parameters, you can also pass parameters from the page by @requestparam $ Users.put (User.getid (), user);Panax Notoginseng return"Success"; - } the +@RequestMapping (value= "/{id}", method=requestmethod.get) A PublicUser getUser (@PathVariable Long id) { the //GET request to process "/users/{id}" to get user information for ID value in URL + //the ID in the URL can be bound to the parameters of the function by @pathvariable - returnusers.get (ID); $ } $ -@RequestMapping (value= "/{id}", method=requestmethod.put) - PublicString putuser (@PathVariable Long ID, @ModelAttribute user user) { the //handle put request for "/users/{id}" to update user information -User U =users.get (ID);Wuyi U.setname (User.getname ()); the U.setage (User.getage ()); - users.put (ID, u); Wu return"Success"; - } About $@RequestMapping (value= "/{id}", method=requestmethod.delete) - PublicString deleteuser (@PathVariable Long id) { - //Delete request to process "/users/{id}" to delete user - users.remove (ID); A return"Success"; + } the -}
Spring-boot "04": Spring Boot Build restful API