有段时间没有写博客,最近入职,准备静下心来好好学习技术,把以前没用的博客删掉,从头开始写博客。本文是介绍springboot如何与springmvc完成合作,而且也只是简单介绍的整合springmvc
首先创建springboot工程,可用idea的自带的生成插件生成
下一步之后可选择自身所需的依赖
接下来我们创建一个controller
package com.example.springboot01.controller;
import com.example.springboot01.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by linjiaming
*/
@RestController
public class HelloWorld {
//读取配置文件中数值
@Value("${boot.name}")
private String name;
@RequestMapping("hello")
public User hello(){
User user = new User();
user.setName(name);
user.setAge("18");
return user;
}
}
定义一个实体类,作为返回前台的json数据
package com.example.springboot01.entity;
/**
* Created by linjiaming
*/
public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
运行项目输入地址访问