简体   繁体   English

如何在rest客户端使用spring @RequestBody发送post请求

[英]How to send post request with spring @RequestBody in rest client

I have a class Person. 我有一个班级的人。

class Person{
Integer id;
String firstName;
String lastName;
//other params, constructors, getters & setters
}

& My method is 我的方法是

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public void testPerson(
            @RequestBody Person person){
...
}

Now I need to test it using rest client. 现在我需要使用rest client测试它。 I tried setting up “request header” section of the Firefox plugin to have a “name” = “Content-Type” and “value” = “application/x-www-form-urlencoded” & then add parameters in body, 我尝试设置Firefox插件的“请求标题”部分,使其具有“name”=“Content-Type”和“value”=“application / x-www-form-urlencoded”然后在body中添加参数,

id=1&firstName=aaa&lastName=bbb

but it's giving 404. 但它给了404。

If you are getting 404 response, this means either your request URL is wrong or you using GET method instead of POST or vise versa. 如果您收到404响应,则表示您的请求URL错误或您使用GET方法而不是POST ,反之亦然。

Then regarding passing Person in request, if @RequestBody is used you have to pass JSON or XML in the body of the request as playload. 然后关于在请求中传递Person ,如果使用@RequestBody ,则必须在请求正文中将JSON或XML作为playload传递。

JSON: JSON:

{
  "id":1,
  "firstName":"aaa",
  "lastName":bbb
}

XML XML

<person>
  <id>1<id>
  <firstName>aaa</firstName>
  <lastName>bbb</lastName>  
</person>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将请求发送到后端 spring rest controller 与 Z466DEEC76ECDF324554 请求文件正文和请求 param - How to send request to backend spring rest controller with a json requestbody and request param as a multipart image file 如何在 POST 请求中使用附加的 @RequestBody 将 Pageable 传递给 Feign 客户端 - How to pass Pageable to Feign Client in POST Request with additional @RequestBody 带有Android客户端的Spring RestService为RequestBody发送对象 - Spring RestService with android client send object for RequestBody 发送请求到Spring Rest API - Send post request to spring rest api 如何在 @ExceptionHandler (Spring REST) 2.0 中获取 @RequestBody - How to get the @RequestBody in an @ExceptionHandler (Spring REST) 2.0 如何在@ExceptionHandler (Spring REST) 中获取@RequestBody - How to get the @RequestBody in an @ExceptionHandler (Spring REST) 从POST请求向Java中的外部GET请求发送自定义RequestBody - send custom RequestBody from POST request to an external GET request in java Spring Contracts:如何将字符串集合作为 RequestBody 发送 - Spring Contracts: how to send a Collection of Strings as a RequestBody spring中如何将@Requestbody和@Requestpart一起发送 - How to send @Requestbody and @Requestpart together in spring 如何在 Spring 引导 Rest Api 中使用参数@RequestBody ZEED8D8342408A85EE633 request 纠正 OverPosting? - How to correct OverPosting in Spring Boot Rest Api with parameter @RequestBody Json request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM