直接写在控制器方法的参数后面,例如username和pwd,代码如下:
@RequestMapping("/hello2")
public String Index(String username,String pwd) {
return username;
}如果post的方式是application/x-www-form-urlencoded,上面可以正常接收,但是如果是application/json的话,就要自定义解析,代码如下:import com.alibaba.fastjson.JSONObject;或者使用使用@RequestBody注解
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@RestController
public class TestController {
@RequestMapping(value = "/demo",method = RequestMethod.POST)
public String demo(HttpServletRequest req){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
StringBuffer sb=new StringBuffer();
String s=null;
while((s=br.readLine())!=null){
sb.append(s);
}
JSONObject jsonObject = JSONObject.parseObject(sb.toString());
String name = jsonObject.getString("name");
String age = jsonObject.getString("age");
System.out.println("name:"+name+" age:"+age);
} catch (IOException e) {
e.printStackTrace();
}
return "server response";
}
}
import org.springframework.web.bind.annotation.RequestMapping;Person类
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/demo",method = RequestMethod.POST)
public String demo(@RequestBody Person person){
System.out.println(person);
return "server response";
}
}
import lombok.*;
@Data
class Person{
private String name;
private String age;
}
网友回复
seedance2如何根据一张九宫格分镜头图片生成ai视频短剧?
www.gstatic.com打开报错net::ERR_TUNNEL_CONNECTION_FAILED
果蝇大脑神经在计算机中复原意味人脑神经和意识也可在计算机中复原?
cosyvoice-v3.5声音克隆报错:Error during speech synthesis: start speech synthesizer failed within 5s.
html如何实现二进制程序转成汇编代码?
python如何通过音色描述和说话内容生成语音?
python如何通过api调用Seedance 2.0 模型生成视频?
如何使用grok imagine免费生成10分钟ai短片?
ai能破解exe文件重写源代码吗?
浏览器除了于Chromium内核还有其他的不用于Chromium内核的浏览器吗?


