初始目录

创建controller和service包

创建DemoController和DemoService

DemoController
注解说明
@Controller注解声明这个类是一个Controller
@RequestMapping注解修饰在class上时其中的值为请求父路径
@RequestMapping注解修饰在方法上时其中的值为请求路径
@@ResponseBody注解修饰在方法上时表示请求这个接口时返回内容而不是返回页面
@ResponseBody修饰在class上时表示请求这个Controller中是所以接口时都返回内容而不是页面
@Service注解修饰在class上表名这个class是一个Service
package com.imsjw.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class DemoController {
@ResponseBody
@RequestMapping("/test")
public String test(){
return "test";
}
}
DemoService
package com.imsjw.demo.service;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
}
启动项目

进行测试
浏览器地址栏输入 http://127.0.0.1:8080/demo/test
默认端口是8080


本文详细介绍了如何使用Spring框架搭建MVC项目,包括创建Controller和服务层,并通过浏览器测试接口返回内容。
1251

被折叠的 条评论
为什么被折叠?



