本篇文章的主要内容是关于项目的跨域配置和给项目添加restful风格接口。
重点是文件粘贴
文章目录
- 一、 跨域
- 二、Restful风格
- 1. 什么是restful风格?
- 2. 项目文件结构
- 3. 新建文件
- 4. 在Controller中进行修改
一、 跨域
跨域问题暂时也就那样,解决方法就是复制粘贴一个不知道从哪里来的CorsConfig文件,这样就可以解决问题:
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/**
* @author oyan
* @date 2024/5/22 17:20
* @description CorsConfig
*/
@Configuration
public class CorsConfig implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin");
if(origin!=null) {
response.setHeader("Access-Control-Allow-Origin", origin);
}
String headers = request.getHeader("Access-Control-Request-Headers");
if(headers!=null) {
response.setHeader("Access-Control-Allow-Headers", headers);
response.setHeader("Access-Control-Expose-Headers", headers);
}
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
二、Restful风格
1. 什么是restful风格?
其实就是长这个样的:
就是返回的参数是这样的,很规整的。
2. 项目文件结构
接下来是这样的结构,你需要新建如下的包
3. 新建文件
新建枚举CodeEnum
/**
* @author oyan
* @date 2024/5/22 17:23
* @description 枚举
*/
public enum CodeEnum {
SUCCESS(200,"操作成功"),
ERROR(400,"error"),
TOKEN_NULL(403,"token不存在"),
USER_INFO_ERROR(405,"用户信息错误"),
FAIL_APPLY(407,"请求失败"),
ROLE_ERROR(406,"角色错误"),
TOKEN_ILLEGAL(408,"token不合法");
int code;
String msg;
CodeEnum(int code, String message){
this.code=code;
this.msg=message;
}
public int getCode(){return code;}
public String getMsg(){return msg;}
public String toString(){
return "\"{code\":\""+ this.code +"\","+
"\"msg\":\""+ this.msg+"\"}";
}
}
新建记录Rest
/**
* @author Administrator
* @date 2024/5/22 17:25
* @description Rest restful风格的结果
*/
public record Rest<T>(int code, String message, T data) {
public static <T> Rest<T> success(T data){return new Rest<>(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),data);}
public static <T> Rest<T> success(){return new Rest<>(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), null);}
public static <T> Rest<T> success(String msg){return new Rest<>(CodeEnum.SUCCESS.getCode(), msg,null);}
public static <T> Rest<T> failure(CodeEnum codeEnum){return new Rest<>(codeEnum.getCode(),codeEnum.getMsg(),null);}
public static <T> Rest<T> failure(String msg){return new Rest<>(400,msg,null);}
}
4. 在Controller中进行修改
以CategoryController为例:
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/all")
public Rest findAll() {
try{
return Rest.success(categoryService.findAll());
}catch (Exception e){
return Rest.failure(e.getMessage());
}
}
@PostMapping("/input")
public Rest insert(@RequestParam("name") String name, @RequestParam("parentCategory") Integer parentCategory, @RequestParam("level") Integer level) {
System.out.println(name+parentCategory+level);
Category category = new Category();
category.setName(name);
category.setParentCategory(parentCategory);
category.setLevel(level);
try {
categoryService.insert(category);
return Rest.success();
}catch(Exception e) {
return Rest.failure(e.getMessage());
}
}
@PostMapping("/delete")
public Rest delete(@RequestParam("id") Integer id) {
try {
categoryService.delete(id);
return Rest.success();
}catch (Exception e){
return Rest.failure(e.getMessage());
}
}
@PostMapping("/find")
public Rest getCategoryById(@RequestParam("id") Integer id) {
try {
Category category = categoryService.getCategoryById(id);
if(category == null){
return Rest.failure("查询结果为空/编号不存在");
}
return Rest.success(category);
}catch (Exception e){
return Rest.failure(e.getMessage());
}
}
}