为什么我们在Rest Api中使用标头,它们是如何在Sping Boot 中使用的,以及Spring Boot的类型是什么,如果有人清楚我的概念,我将非常感谢,我搜索了很多,但未能理解。以下是我如何使用
@RestController
@RequestMapping
public class postapi {
PreparedStatement ps = null;
@PostMapping("/employee")
public ResponseEntity<String> insertData(@RequestBody Employee employee,
//this is my header @RequestHeader (name = "testName", required = false) String headerPersist){
try{String query = "insert into employee(emp_id,name,age,dep_id)"+"values(?,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DBUtilities.getConnection();
ps = con.prepareStatement(query);
ps.setInt(1,employee.getEmp_id());
ps.setString(2,employee.getName());
ps.setInt(3,employee.getAge());
ps.setInt(4,employee.getDep_id());
int records_inserted = ps.executeUpdate();
System.out.println(("Executed"));
}
catch (ClassNotFoundException e) {
System.out.println(("ClassNotFoundException occurs"));
throw new RuntimeException(e);
} catch (SQLException e) {
System.out.println(("you got an error in your sql syntax"));
throw new RuntimeException(e);
}
return new ResponseEntity( HttpStatus.OK);
}
}
1条答案
按热度按时间ql3eal8s1#
一些头是有用的,如认证。当你在你的应用程序中传递令牌时,你不能要求用户输入令牌到参数的主体中。它必须从浏览器头发送