Spring-Boot框架RestFul设计风格改进请求访问------Spring-Boot框架

文章介绍了使用SpringBoot创建RESTfulAPI,包括GET和POST方法处理用户信息、文件上传功能,以及如何实现登录拦截器。还讨论了multipart配置,如文件上传大小限制和静态资源路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.powernode.springbootvue.controller;

import com.powernode.springbootvue.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class ParamsController
{
    @GetMapping("/user/get/{id}")
    public String get(@PathVariable int id)
    {
        System.out.println("我在这");
        System.out.println(id);
        return "获取用户信息成功";
    }
    @RequestMapping("/upload")
    public String up(String nickname, MultipartFile photo, HttpServletRequest request)
    {
        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());
        System.out.println(System.getProperty("user.dir"));
        String path = request.getServletContext().getRealPath("/upload/");
        System.out.println(path);
        try {
            saveFile(photo,path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "上传成功";
    }
    public void saveFile(MultipartFile photo,String path) throws IOException {
        File dir = new File(path);
        if(!dir .exists())
        {
            dir.mkdir();
        }
        File file = new File(path + photo.getOriginalFilename());
        photo.transferTo(file);
    }
    @RequestMapping("/user/helloGet")
    public String helloGet(@RequestParam(value = "nickname",required = false) String name)
    {
        return name;
    }
    @RequestMapping("/user/helloPost")
    public String helloPost(@RequestParam(value = "nickname",required = false) String name,String password)
    {
        System.out.println(name + password);
        return name;
    }
    @RequestMapping(value = "/PostTest",method = RequestMethod.POST)
    public void PostTest(User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping(value = "/PostTest1",method = RequestMethod.POST)
    //如果前端回传的json类型的数据,需要添加上@RequestBody这个注解,否则无法接收
    public void PostTest1(@RequestBody User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping("/user/**")
    public String test()
    {
        return "通配符请求";
    }
}
package com.powernode.springbootvue.controller;

import com.powernode.springbootvue.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class ParamsController
{
    @GetMapping("/user/get/{id}")
    public String get(@PathVariable int id)
    {
        System.out.println("我在这");
        System.out.println(id);
        return "获取用户信息成功";
    }
    @RequestMapping("/upload")
    public String up(String nickname, MultipartFile photo, HttpServletRequest request)
    {
        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());
        System.out.println(System.getProperty("user.dir"));
        String path = request.getServletContext().getRealPath("/upload/");
        System.out.println(path);
        try {
            saveFile(photo,path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "上传成功";
    }
    public void saveFile(MultipartFile photo,String path) throws IOException {
        File dir = new File(path);
        if(!dir .exists())
        {
            dir.mkdir();
        }
        File file = new File(path + photo.getOriginalFilename());
        photo.transferTo(file);
    }
    @RequestMapping("/user/helloGet")
    public String helloGet(@RequestParam(value = "nickname",required = false) String name)
    {
        return name;
    }
    @RequestMapping("/user/helloPost")
    public String helloPost(@RequestParam(value = "nickname",required = false) String name,String password)
    {
        System.out.println(name + password);
        return name;
    }
    @RequestMapping(value = "/PostTest",method = RequestMethod.POST)
    public void PostTest(User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping(value = "/PostTest1",method = RequestMethod.POST)
    //如果前端回传的json类型的数据,需要添加上@RequestBody这个注解,否则无法接收
    public void PostTest1(@RequestBody User user)
    {
        System.out.println(user.toString());
    }
    @RequestMapping("/user/**")
    public String test()
    {
        return "通配符请求";
    }
}
package com.powernode.springbootvue.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor");
        //true是放行,false是拦截
        return true;
    }
}
package com.powernode.springbootvue.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor");
        //true是放行,false是拦截
        return true;
    }
}
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.web.resources.static-locations=/upload/
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.web.resources.static-locations=/upload/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值