创建第一个spring boot

一 main方法

@SpringBootApplication

spring boot 项目开启的注解,是spring boot项目的入口

2、controller

@Controller    @ResponseBody    @RequestMapping

 

java

package com.dwb.controller;

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

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MapController {

    @RequestMapping("map")
    @ResponseBody
    public Map<String,Object> getMap(){
        Map<String,Object> map = new HashMap();
        
        map.put("name", "dwb");
        return map;
    }
}

 

package com.dwb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {
    
    //带有 requestMapping注解的访问路径
    //浏览器访问地址是http://localhost:8080/index1.html
    //页面显示: index
    @RequestMapping("index1.html")
    public @ResponseBody String indexHtml() {
        return "index";
    }
    
    /**
     * 没有使用注解
     * 访问路径是:http://localhost:8080/index.html
     * 页面显示:hello spring boot
     * @return
     */
    public String index() {
        return "index";
    }
}
将spring boot项目默认的上下文“/”和端口号"8080"修改为“/webapi”和“8081”

#设置tomcat的内置端口号 server.port
=8081 #设置上下文根 server.servlet.context-path=/webapi

 

posted @ 2022-03-07 18:13  hotdog-dou  阅读(21)  评论(0)    收藏  举报