简体   繁体   English

使用Post方法Rest + SpringBoot

[英]Consume Post method Rest + SpringBoot

I have been searching all morning and i think i'm missing something . 我整个上午都在搜寻,我想我缺少了一些东西。
i have a Spring boot controller with a method to save a client. 我有一个带有方法来保存客户端的Spring Boot控制器。
this is the method : 这是方法:

// ajouter un client
@RequestMapping(value="/AjoutClient/{clientData}", method=RequestMethod.POST)
public String AjoutClient(@PathVariable String clientData) {

    Client c = new Client();
    c.setNomClient(clientData.split(";")[0]);
    c.setPrenomClient(clientData.split(";")[1]);
    c.setAdresseClient(clientData.split(";")[2]);
    c.setTelClient(clientData.split(";")[3]);
    c.setEmailClient(clientData.split(";")[4]);
    c.setCinClient(clientData.split(";")[5]);

    client.save(c);
    return "test";
}

i want to consume this method from another application with this method : 我想从另一个应用程序使用此方法使用此方法:

@RequestMapping(value="/ajoutClient", method=RequestMethod.POST)
 public void ajout(@RequestParam("nom") String nom,@RequestParam("prenom") String prenom,@RequestParam("adr") String adr,@RequestParam("tel") String tel,@RequestParam("mail") String mail,@RequestParam("cin") String cin) {

    String ClientData=nom+";"+prenom+";"+adr+";"+tel+";"+mail+";"+cin;

     RestTemplate restTemplate = new RestTemplate();

     HttpEntity<String> request = new HttpEntity<>(new String(ClientData));
     ResponseEntity<String> response = restTemplate
       .exchange("http://localhost:9093/AjoutClient/"+ClientData, HttpMethod.POST, request, String.class);

     assertThat(response.getStatusCode(), is(HttpStatus.CREATED));

  }

** explication : i get the values from a form and construct a string with those values, then try to send that string to my clientController. **说明:我从表单中获取值并使用这些值构造一个字符串,然后尝试将该字符串发送到我的clientController。
PS: i can't send client object, i have to send the values one by one then create the client object in the clientController. PS:我无法发送客户端对象,我必须一个接一个地发送值,然后在clientController中创建客户端对象。
i'm feeling pretty lost because i can see that something is wrong but i don't know what is it. 我感到非常迷茫,因为我可以看到有些问题,但是我不知道这是什么。

First of all I would suggest you avoid using @PathVariable for passing the data like this. 首先,我建议您避免使用@PathVariable传递数据。 You're already sending everything in the request body, so first step is to change: 您已经在请求正文中发送了所有内容,因此第一步是更改:

public String AjoutClient(@PathVariable String clientData) {

to

public String AjoutClient(@RequestBody String clientData) {

and

restTemplate.exchange("http://localhost:9093/AjoutClient/" + ClientData, HttpMethod.POST, request, String.class);

to just 只是

restTemplate.exchange("http://localhost:9093/AjoutClient", HttpMethod.POST, request, String.class);

Then if you're expecting 201 status then you have to return it: 然后,如果您希望获得201状态,则必须返回它:

public ResponseEntity<String> AjoutClient(@RequestBody String clientData) {
    ...
    return ResponseEntity.created(null).body("test");
}

PS: Please pay attention to what @JB Nizet mentioned, cause he has a point here. PS:请注意@JB Nizet提到的内容,因为他在这里有意思。 Just research that keywords (google them) or read some tutorials eg https://www.baeldung.com/java-url-encoding-decoding or https://www.baeldung.com/rest-template and you'll easily find out more about standard practices. 只需研究该关键字(用Google搜索)或阅读一些教程(例如https://www.baeldung.com/java-url-encoding-decodinghttps://www.baeldung.com/rest-template) ,您就可以轻松找到进一步了解标准做法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM