引入@SpringBootApplication
写一个main方法SpringApplication.run(Xxx.class,args) 启动项目
package com.sdcet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestaurantMain {
public static void main(String[] args) {
SpringApplication.run(RestaurantMain.class, args);
}
}
定义一个Phone类 设置静态方法 设置多个变量
package com.sdcet.entity;
public class Phone {
private String brand=“名牌手机”;
private String model=“最新款”;
private double price=1000.2;
private boolean newest=true;
}
定义Webstie 使用静态方法 并生成getter setter方法
package com.sdcet.entity;
public class Webstie {
private String name;
private String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
设置Srapper类 获取网站连接数
private List sites;
并生成getter setter方法
package com.sdcet.entity;
import java.util.List;
public class Wrapper {
private List sites;
public void setSites(List<Webstie> sites){
this.sites=sites;
}
public List<Webstie> getSites(){
return sites;
}
}
设置类HelloControlller,引入
@Controller
@ResponseBody
使用@RequestMapping 注解映射请求路径
并定义微博 设置setName setUrl
package com.sdcet.constroller;
import com.sdcet.entity.Webstie;
import com.sdcet.entity.Wrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@ResponseBody
public class HelloControlller {
@RequestMapping("/hello")
public String hello() {
return “HELLO SPRINGBO!!!”;
}
@RequestMapping("/getSites")
public Wrapper getSites (){
Wrapper wrapper = new Wrapper();
List<Webstie> webstieList=new ArrayList<>();
Webstie webstie = new Webstie();
webstie.setName("菜鸟教程");
webstie.setUrl("www.runoob.com");
webstieList.add(webstie);
Webstie webstie1 = new Webstie();
webstie1.setName("google");
webstie1.setUrl("www.gooogle.com");
webstieList.add(webstie1);
Webstie webstie2 = new Webstie();
webstie2.setName("微博");
webstie2.setUrl("www.weibo.com");
webstieList.add(webstie2);
wrapper.setSites(webstieList);
return wrapper;
}
}