Spring Boot—11 Controller Controller


package com.bee.sample.smartmap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bee.sample.smartmap.entity.User;
import com.bee.sample.smartmap.service.UserService;
/**
 * url maps to methods
 *
 */
@Controller
@RequestMapping("/user4")
public class Sample34Controller {
    
    @Autowired UserService userService;
    
    @GetMapping("/" )
    public  @ResponseBody String index() {
        return "hell";
    }
    

    
    /**
     * Client requests must contain application/json to be processed
     * @return
     */
    @GetMapping(value="/all1.json",consumes = "application/json" )
    @ResponseBody
    public   User forJson() {
        return userService.getUserById(1l);
    }
    
    @GetMapping(path = "/user/{userId}.json", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public User getUser(@PathVariable Long userId, Model model) {
         return userService.getUserById(userId);
    }
    
    
    @GetMapping(path = "/update.json", params = "action=save")
    @ResponseBody
    public void saveUser() {
         System.out.println("call save");
    }
    
    @GetMapping(path = "/update.json", params = "action=update")
    @ResponseBody
    public void updateUser() {
         System.out.println("call update");
    }
    
    
    
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324716246&siteId=291194637