首先我用的是开发工具是idea,(eclipse)应该是同理的。
1,开始:
下一步,然后finishi;
2,配置文件,不用修改,暂时默认就好
4.0.0 com.example demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
3,在之前设定的Package下进行文件的新建(若不在此目录下,Controller层的RequestMapping将捕获不到请求)
新建一个Controller类,MainController.
package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by gzj on 18/3/6. */@Controller@RequestMapping("/service")public class MainController { @RequestMapping(value = "/report/spiderList", method = RequestMethod.GET) @ResponseBody @CrossOrigin public void spiderLisSearch() { int a = 0; } @RequestMapping(value = "/report/general", method = RequestMethod.POST) @ResponseBody @CrossOrigin public void generalSearch(@RequestBody String name) { int a = 0; }}
3,用postman发送一个请求
代码将会捕获到这个请求。
遇见的坑:
1,新建的类一定要放在Package(第二张截图)指定的目录下。
2,若想用实体类来接受参数,类中一定要有set(),get()方法,否则无法接受参数
3,
@CrossOrigin
这个是跨域时需要加的,以后会解释