SpringBoot Controller接收参数的几种常用方式

SpringBoot Controller接收参数的几种常用方式

第一类:请求路径参数

  • @PathVariable

    • 获取路径参数。即url/{id}
  • @RequestParam

    • 获取查询参数。即url?name=小布
//GET
http://localhost:8080/linjing/3?name=小布

//____________________________________________________________________
@GetMapping("/linjing/{id}")
public void test(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
    System.out.println("id="+id);
    System.out.println("name="+name);
}
//输出结果 
id=3
name=小布

第二类:Body参数

*‘content-type’ : application/json*

@PostMapping(path = "/linjing")
public void test(@RequestBody Person person) {
    System.out.println(person.toString());
}
@PostMapping(path = "/linjing")
public void test(@RequestBody Map<String, String> person) {
    System.out.println(person.get("name"));
}

*‘content-type’ : form-data*

@PostMapping(path = "/linjing")
public void test(Person person) {
    System.out.println(person.toString());
}

’content-type’ :x-www-form-urlencoded

//multipart/form-data与x-www-form-urlencoded的区别:
//multipart/form-data:可以上传文件或者键值对,最后都会转化为一条消息
//x-www-form-urlencoded:只能上传键值对,而且键值对都是通过&间隔分开的。

第三类:请求头参数以及Cookie

@GetMapping("/linjing")
public void test(@RequestHeader(name = "myHeader") String myHeader,
        @CookieValue(name = "myCookie") String myCookie) {
    System.out.println("myHeader=" + myHeader);
    System.out.println("myCookie=" + myCookie);
}
@GetMapping("/linjing")
public void test(HttpServletRequest request) {
    System.out.println(request.getHeader("myHeader"));
    for (Cookie cookie : request.getCookies()) {
        if ("myCookie".equals(cookie.getName())) {
            System.out.println(cookie.getValue());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Spring Boot中,接收HTTP请求参数乱码通常是由于默认字符编码不正确所致。解决该问题的方法有以下几种: 1. 使用RequestMapping或GetMapping注解的方法时,可在注解中指定produces和consumes属性,并指定字符编码。例如: ```java @RequestMapping(value = "/example", produces = "application/json;charset=UTF-8", consumes = "application/json;charset=UTF-8") public String example(@RequestBody String requestParam) { // 处理请求参数 } ``` 2. 修改Spring Boot应用的全局字符编码设置,可在application.properties或application.yml文件中配置。例如在application.properties中添加: ``` spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true ``` 3. 可以通过在Spring Boot的启动类中添加Filter或Interceptor,手动处理请求参数的字符编码。例如创建一个字符编码过滤器: ```java @Component public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } @Override public void destroy() {} } ``` 通过以上方法,可以解决Spring Boot接收HTTP请求参数乱码的问题。在实际应用中,根据具体需求选择适合的解决方案。 ### 回答2: 在Spring Boot中,接收HTTP请求参数乱码的问题通常是由于字符编码不一致导致的。可以通过以下几种方式来解决: 1. 使用过滤器(Filter): 可以在Spring Boot中注册一个字符编码过滤器,通过将所有的请求和响应的字符编码都设置为UTF-8来避免乱码问题。在Spring Boot中可以通过重WebMvcConfigurer接口中的addInterceptors方法来注册过滤器。 2. 在application.properties(application.yml)文件中设置字符编码: 可以通过在application.properties(application.yml)文件中添加以下配置来设置字符编码: spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true 3. 使用@RequestParam注解指定字符编码: 可以在Controller中的接收参数的方法上使用@RequestParam注解,并通过设置其value属性来指定字符编码。 例如: @RequestMapping("/test") public String test(@RequestParam(value = "name", required = false) String name) { // ...业务逻辑 return "success"; } 4. 在请求中指定字符编码: 可以在发送HTTP请求时,在请求中指定字符编码为UTF-8。例如,在使用HttpClient发送请求时,可以使用setHeader方法设置字符编码。 例如: HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // ...设置请求参数 HttpResponse response = httpClient.execute(post); 通过以上几种方式,可以解决Spring Boot接收HTTP请求参数乱码的问题。 ### 回答3: 当Spring Boot接收到HTTP请求参数乱码的情况时,可以采取以下措施解决问题。 首先,可以在Spring Boot的配置文件application.properties(或application.yml)中添加以下配置,设置请求编码格式为UTF-8: ``` spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true ``` 同时,可以使用过滤器对请求进行编码处理。在新建一个类中,实现javax.servlet.Filter接口,并重doFilter方法: ``` import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "encodingFilter", urlPatterns = "/*") public class EncodingFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } } ``` 在上述代码中,设置请求和响应的编码格式为UTF-8。 然后,可以为Spring Boot的主类添加一个注解,启用过滤器: ``` import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在上述代码中,使用了@SpringBootAppliaction注解标记为Spring Boot主类,并使用@ServletComponentScan注解扫描过滤器。 最后,可以在控制器中使用@RequestParam注解显式指定请求参数的编码格式。例如: ``` import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(@RequestParam(value = "name", required = false) String name) { if (!StringUtils.isEmpty(name)) { // 对name参数进行进一步处理 } return "Hello, " + name; } } ``` 在上述代码中,使用@RequestParam注解指定了name参数,并设置了编码格式。 通过以上措施,可以解决Spring Boot接收HTTP请求参数乱码的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值