SpringBoot GET POST 简单搭建

xiaoxiao2021-02-28  98

概述

本文简单记录了使用Spring Boot搭建服务器的过程

1. 在eclipse搭建Maven 项目

 

2. maven 配置文件pom.xml增加依赖包

 

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version>

3. 编写代码, 我的代码结构如下

 

4. 编写启动入口

@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

5. 编写最简单的Hello接口, 接口定位都放在controller包中

@Controller @RequestMapping("/firstSpringBoot") public class ResponseController { @RequestMapping("/hello") public String hello() { return "Hello"; }

 

Controller 是 Spring 中最基本的组件,主要是处理跟用户交互的,一般每个业务逻辑都会有一个 Controller,提供给用户 http 请求接口,用户请求接口进行数据访问。 Controller有以下有些常用的注解

@Controller:标注 Controller 类,处理 http 请求

@RestController = @Controller + @ResponseBody主要是为了使 http 请求返回数据格式为 json 格式,一帮情况下使用这个注解

@RequestMapping:配置 url 映射,可以作用于类上,也可以在方法上

以下接口 在访问http://localhost:8080/firstSpringBoot/hello  会返回"Hello"字段

@RequestMapping  可以指定GET,POST,DELETE,PUT四种标准的 Restfulapi请求方法  method没有指定默认为get请求

@GetMapping = @RequestMapping  +  method = RequestMethod.GET

@PostMapping = @RequestMapping + method = RequestMethod.POST

 

@RequestMapping(value= "/getResponse" , method = RequestMethod.GET) public Response getResponse() { return responseServiceImpl.getResponse("test"); }

如果返回的是对象, 那会默认将对象转换为Json格式返回

public class Response { private int code; private String message; private List<String> messages; 。。。。。。 }

返回结果:

 

@PathVariable @RequestParam都可以用于GET请求加参数,以下是两者的区别

使用@RequestParam接收参数,如下图

 

@GetMapping(value= "/getMessagePath") public Response getResponse(@RequestParam(value="message") String message) { return responseServiceImpl.getResponse(message); }

url结构:

 

url=.../getMessagePath?message=${message}

@PathVariable

 

@RequestMapping(value= "/getMessage/{message}", method = RequestMethod.GET) public Response getMessage(@PathVariable(value="message") String message) { return responseServiceImpl.getResponse(message); }

url结构:url=.../getMessage/${message}  占位符的方式POST 请求可以使用如下几种方式:

 

 

@RequestMapping(value= "/getMessage", method = RequestMethod.POST) public Response getMessage(int code, String message) { return responseServiceImpl.getResponse(code, message); }

发送该请求如下:

 

也可以使用如下形式发送请求   

POST使用 @RequestBody

 

@PostMapping(value= "/getMessageBody") public Response getMessagePost(@RequestBody PostBean bean) { return responseServiceImpl.getResponse(bean.getCode(), bean.getMessage()); } public class PostBean { private int code; private String message; private List<String> messages; 。。。。 }

使用以下方式请求

 

3. 运行方式

 

Spring boot自带Tomcat作为服务器

书写以下类作为启动入口(@SpringBootApplication作为注解)

 

@SpringBootApplication public class TestHello { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(TestHello.class, args); } }

 

@c用于标注业务层组件

 

@Service public class ResponseServiceImpl implements IResponseService{ @Autowired private ResponseDaoImpl responseDaoImpl; @Override public Response getResponse(String message) { return responseDaoImpl.getResponse(message); } @Override public Response getResponse(int code, String message) { // TODO Auto-generated method stub return responseDaoImpl.getResponse(code, message); } }

 

@controller 中注入@Service

 

@RestController @RequestMapping("/firstSpringBoot") public class ResponseController { @Autowired IResponseService responseServiceImpl; 。。。 }

 

如果需要使用注解方式注入, 那注入的类必须使用注解

 

@Service public class ResponseServiceImpl implements IResponseService{ @Autowired private ResponseDaoImpl responseDaoImpl; 。。。 }

 

@Repository public class ResponseDaoImpl implements IResponseDao{

运行启动类

 

启动以后使用localhost:8080来发送请求。

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-48947.html

最新回复(0)