Spring Boot—11控制器Controller

Posted ParamousGIS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot—11控制器Controller相关的知识,希望对你有一定的参考价值。


package com.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.sample.smartmap.entity.User;
import com.sample.smartmap.service.UserService;
/**
 * url映射到方法
 *
 */
@Controller
@RequestMapping("/user4")
public class Sample34Controller {
    
    @Autowired UserService userService;
    
    @GetMapping("/" )
    public  @ResponseBody String index() {
        return "hell";
    }
    

    
    /**
     * 客户端请求必须包含application/json 才会处理
     * @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");
    }
    
    
    
}

以上是关于Spring Boot—11控制器Controller的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot—20Zookeeper

Spring Boot引入Lombok

带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)

Spring Boot-日志配置(超详细)

Spring Boot—15SpringJPA

Spring Boot—14JdbcTemplate