此示例以代码方式展现,可直接结合controller层每个接口上方注释与其接口传递参数方式理解!
前端传参直接就以apiPost工具来代替
apiPost调用后端接口几种方式
代码:
controller层:
package com.chensir.ceshi.controller;
import com.chensir.ceshi.entity.User;
import com.chensir.ceshi.service.GetAndPostService;
import com.chensir.handler.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/testGetAndPost")
public class GetAndPostController {
@Resource
private GetAndPostService getAndPostService;
/**
* 1、可查询全部(什么都不传):http://localhost:8088/testGetAndPost/select1
* 2、body json方式 值传不过来
* 3、可 ?拼接(query):http://localhost:8088/testGetAndPost/select1?sex=男
* 4、body中form-data 值能传过来 可以查询 x-www-form-urlencoded 值传不过来 不能查询
*
*/
@GetMapping("/select1")
public Result<Object> selectUser(User user) {
List<User> userList= getAndPostService.selectUser(user);
return Result.ok(userList);
}
/**
* 1、查询全部(什么都不传)提示错误请求:http://localhost:8088/testGetAndPost/select2
* 2、body json方式:可以查询
* 3、?拼接(query)提示错误请求:http://localhost:8088/testGetAndPost/select1?sex=男
* 4、body中form-data、x-www-form-urlencoded 提示错误请求
*
*/
@GetMapping("/select2")
public Result<Object> selectUser1(@RequestBody User user) {
List<User> userList= getAndPostService.selectUser(user);
return Result.ok(userList);
}
/**
* 1、body json方式:值传不过来 查询为空
* 3、?拼接(query)可以查询:http://localhost:8088/testGetAndPost/select1?name=张三
* 4、body中form-data:可以查询; x-www-form-urlencoded:值传不过来 查询为空
*
*/
@GetMapping("/select3")
public Result<Object> selectUser2(String name) {
List<User> userList= getAndPostService.selectUserByName(name);
return Result.ok(userList);
}
/**
* 1、body json方式:提示错误请求
* 3、?拼接(query)可以查询:http://localhost:8088/testGetAndPost/select1?name=张三
* 4、body中form-data:可以查询; x-www-form-urlencoded:提示错误请求
*
*/
@GetMapping("/select4")
public Result<Object> selectUser3(@RequestParam String name) {
List<User> userList= getAndPostService.selectUserByName(name);
return Result.ok(userList);
}
/**
* 1、只能以这样的方式:http://localhost:8088/testGetAndPost/selectUser/张三
*
*/
@GetMapping("/selectUser/{name}")
public Result<Object> selectUser4(@PathVariable("name") String name) {
List<User> userList= getAndPostService.selectUserByName(name);
return Result.ok(userList);
}
/**
* 和select3一样
*
*/
@GetMapping("/select5")
public Result<Object> selectUser5(String name,Integer age) {
List<User> userList= getAndPostService.selectUserByName1(name,age);
return Result.ok(userList);
}
/**
* 和select4一样
*
*/
@GetMapping("/select6")
public Result<Object> selectUser6(@RequestParam("name") String name,@RequestParam("age") Integer age) {
List<User> userList= getAndPostService.selectUserByName1(name,age);
return Result.ok(userList);
}
/**
* 1、只能以这样的方式:http://localhost:8088/testGetAndPost/selectUser/张三/18
*
*/
@GetMapping("/selectUser/{name}/{age}")
public Result<Object> selectUser7(@PathVariable("name") String name,@PathVariable("age") Integer age) {
List<User> userList= getAndPostService.selectUserByName1(name,age);
return Result.ok(userList);
}
/**
* 1、body json方式:值传不过来
* 3、?拼接(query)值能传来,能新增:http://localhost:8088/testGetAndPost/select1?name=张三&age=18
* 4、body中form-data、x-www-form-urlencoded:值都能传来,都可以新增
*
*/
@PostMapping("/add1")
public Result<Object> addUser(User user){
String msg=getAndPostService.addUser(user);
return Result.ok(msg);
}
/**
* 只能以body json
*
*/
@PostMapping("/add2")
public Result<Object> addUser1(@RequestBody User user){
String msg=getAndPostService.addUser(user);
return Result.ok(msg);
}
/**
* 1、body json方式:值传不过来
* 3、?拼接(query)值能传来,能新增:http://localhost:8088/testGetAndPost/select1?name=张三
* 4、body中form-data、x-www-form-urlencoded:值都能传来,都可以新增
*
*/
@PostMapping("/add3")
public Result<Object> addUser2(@RequestParam("name") String name){
String msg=getAndPostService.addUserName(name);
return Result.ok(msg);
}
/**
* 只能以 http://localhost:8088/testGetAndPost/selectUser/张三
*
*/
@PostMapping("/add4/{name}")
public Result<Object> addUser3(@PathVariable("name") String name){
String msg=getAndPostService.addUserName(name);
return Result.ok(msg);
}
}
service层:
public interface GetAndPostService {
List<User> selectUser(User user);
List<User> selectUserByName(String name);
List<User> selectUserByName1(String name, Integer age);
String addUser(User user);
String addUserName(String name);
}
@Service
public class GetAndPostServiceImpl implements GetAndPostService {
@Resource
private GetAndPostMapper getAndPostMapper;
@Override
public List<User> selectUser(User user) {
System.out.println(user.getName());
return getAndPostMapper.selectUserList(user);
}
@Override
public List<User> selectUserByName(String name) {
return getAndPostMapper.selectUserByName(name);
}
@Override
public List<User> selectUserByName1(String name, Integer age) {
return getAndPostMapper.selectUserByName1(name,age);
}
@Override
public String addUser(User user) {
if (user==null){
return "user为空";
}
int insert = getAndPostMapper.insert(user);
return insert>0?"ok":"false";
}
@Override
public String addUserName(String name) {
if (name==null){
return "name为空";
}
User user= User.builder()
.name(name)
.build();
int insert = getAndPostMapper.insert(user);
return insert>0?"ok":"false";
}
}
mapper层:文章来源:https://www.toymoban.com/news/detail-730085.html
public interface GetAndPostMapper extends BaseMapper<User> {
List<User> selectUserList(@Param("query") User user);
List<User> selectUserByName(String name);
List<User> selectUserByName1(String name, Integer age);
}
xml:文章来源地址https://www.toymoban.com/news/detail-730085.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chensir.ceshi.mapper.GetAndPostMapper">
<select id="selectUserList" resultType="com.chensir.ceshi.entity.User">
select * from user
where 1=1
<if test="query.name != null and query.name != ''">
and name = #{query.name}
</if>
<if test="query.age != null">
and age = #{query.age}
</if>
<if test="query.sex != null and query.sex != ''">
and sex = #{query.sex}
</if>
</select>
<select id="selectUserByName" resultType="com.chensir.ceshi.entity.User">
select * from user where name=#{name}
</select>
<select id="selectUserByName1" resultType="com.chensir.ceshi.entity.User">
select * from user where name=#{name} and age=#{age}
</select>
</mapper>
到了这里,关于Java中GET请求与POST请求,前端传参与后端接收实例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!