如何使用Postman在Java中向API进行POST请求?

huangapple go评论65阅读模式
英文:

How to POST with Postman to an API in Java?

问题

我尝试创建一个小的API来进行测试,我的Java代码似乎没问题,在Postman中,当我想要进行GET请求来获取我的产品列表时,一切都正常,但当我想要进行POST请求时,出现了400 Bad Request错误。

这是我的代码:

// 产品列表
@GetMapping(value = "/Produits")
public List<Product> listeProduits() {
    return productDao.findAll();
}

// 通过ID获取产品
@GetMapping(value = "/Produits/{id}")
public Product afficherUnProduit(@PathVariable int id) {
    return productDao.findById(id);
}

@PostMapping(value = "/Produits/")
public void ajouterProduit(@RequestBody @Validated Product product, final HttpServletRequest request, Error filterError) {
    productDao.save(product);
}

在Postman中,在请求的Body部分,我已经设置了Content-Type为application/json。

这个问题可能来自哪里?

谢谢。

英文:

I tried to create a small API to do tests, my Java code seems OK, with Postman when I want to do a GET my list of products is fine for me, on the other hand when I want to do a POST, I have a 400 Bad Request.

Here is my code:

// Produits
    @GetMapping(value = &quot;/Produits&quot;)
    public List&lt;Product&gt; listeProduits() {
        return productDao.findAll();
    }

    // R&#233;cup&#232;re un produit par son ID
    @GetMapping(value = &quot;/Produits/{id}&quot;)
    public Product afficherUnProduit(@PathVariable int id) {
        return productDao.findById (id);
    }

    @PostMapping(value = &quot;Produits/&quot;)
    public void ajouterProduit(@RequestBody @Validated Product product, final HttpServletRequest request, Error filterError) {
        productDao.save(product);
    }

In Postman, in my body I have the content-type: application / json of filled in.

Where can this problem come from?

Thanks

答案1

得分: 1

我查看了你的项目。一切都正常工作。但是你的“POST”网址有问题 - “/Produits/” <--- 最后的斜杠可能是长时间不理解和“为什么这不起作用!!”的原因。因为在Postman中,你也必须调用网址 - "localhost:8080/Produits/" <--- 这里也需要最后的斜杠。

我启动了你的项目,并且使用Postman的这个设置可以正常工作(下面是图片)。请仔细查看,每个符号都可能是原因。

如何使用Postman在Java中向API进行POST请求?

如何使用Postman在Java中向API进行POST请求?

在POST和保存之后,我可以通过GET方法获取实体。

如何使用Postman在Java中向API进行POST请求?

英文:

I looked at you project. Everything is working ok.
But you have "bad" POST url - "/Produits/" <--- last slash can be reason for the long not-understanding and "Why this is not working!!". Because in Postman you also must call url -> "localhost:8080/Produits/" <-- here also need last slash.

I've started your project and it works well with this setting of Postman (picrures below). Please look carefully, every symbol is might be a reason.

如何使用Postman在Java中向API进行POST请求?

如何使用Postman在Java中向API进行POST请求?

After POST and save I was able to get entity through GET method

如何使用Postman在Java中向API进行POST请求?

答案2

得分: 0

我尝试重现获得400错误请求,但失败了。

根据您的代码,我可以访问以下地址(您可以尝试这个):

localhost:8080/Produits/

但是

localhost:8080/Produits

返回404未找到。

也许您可以添加更多信息,这可能会有所帮助。谢谢。

英文:

I tried to reproduce getting 400 bad request, but I failed.

Based on your code, I can access to (you might try this)

localhost:8080/Produits/

but

localhost:8080/Produits

returned 404 not found.

Maybe you can add more information, which might be helpful. Thanks.

答案3

得分: 0

400 是坏请求。也许您发送了错误的 POST 请求,而不是 GET 请求。因为只有 "ajouterProduit" 方法有验证。

另外,请检查您的 URL,如果您的 Postman 设置了 "Produits/",请在 Postman 中设置相同的 URL:
http://localhost:8080/Produits/

如果您使用 Linux 或 Mac,您可以通过 curl 检查您的服务:
curl http://localhost:8080/Produits/
这将帮助您理解,您是在发送错误的 Postman 请求,还是服务的 URL 有些不同,或者其他原因。

英文:

400 is Bad request. Maybe you do wrong POST request instead GET. Because only "ajouterProduit" method has validation.

Also yeah check your url- if you have "Produits/" set the same in Postman
http://localhost:8080/Produits/

And you can check you service through curl if you has Linux or Mac:
curl http://localhost:8080/Produits/
It will help you understand - you doing wrong Postman request or service has some different url or something

答案4

得分: 0

以下是要翻译的代码部分:

@Override
public List<Product> findAll() {
    return products;
}

@Override
public Product findById(int id) {
    for (Product product : products) {
        if (product.getId() == id) {
            return product;
        }
    }
    return null;
}

@Override
public Product save(Product product) {
    products.add(product);
    return product;
}

和 Postman 屏幕截图部分的描述:

如何使用Postman在Java中向API进行POST请求?

英文:

And more code :

@Override
    public List&lt;Product&gt; findAll () {
        return products;
    }

    @Override
    public Product findById (int id) {
        for (Product product : products) {
            if (product.getId () == id) {
                return product;
            }
        }
        return null;
    }


    @Override
    public Product save (Product product) {
        products.add(product);
        return product;
    }

And Postman screen :

如何使用Postman在Java中向API进行POST请求?

答案5

得分: 0

以下是翻译好的部分:

// 产品类
public class Product {

    private int id;
    private String nom;
    private int prix;
    private int dureeDeVie;

    public Product() {
    }

    public Product(int id, String nom, int prix, int dureeDeVie) {
        this.id = id;
        this.nom = nom;
        this.prix = prix;
        this.dureeDeVie = dureeDeVie;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public int getPrix() {
        return prix;
    }

    public void setPrix(int prix) {
        this.prix = prix;
    }

    public int getDureeDeVie() {
        return dureeDeVie;
    }

    public void setDureeDeVie(int dureeDeVie) {
        this.dureeDeVie = dureeDeVie;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", nom='" + nom + '\'' +
                ", prix=" + prix +
                ", dureeDeVie=" + dureeDeVie +
                '}';
    }

}
英文:

The Product Class :

public class Product {
private int id;
private String nom;
private int prix;
private int dureeDeVie;
public Product() {
}
public Product (int id, String nom, int prix, int dureeDeVie) {
this.id = id;
this.nom = nom;
this.prix = prix;
this.dureeDeVie = dureeDeVie;
}
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public String getNom () {
return nom;
}
public void setNom (String nom) {
this.nom = nom;
}
public int getPrix () {
return prix;
}
public void setPrix (int prix) {
this.prix = prix;
}
public int getDureeDeVie () {
return dureeDeVie;
}
public void setDureeDeVie (int dureeDeVie) {
this.dureeDeVie = dureeDeVie;
}
@Override
public String toString () {
return &quot;Product{&quot; +
&quot;id=&quot; + id +
&quot;, nom=&#39;&quot; + nom + &#39;\&#39;&#39; +
&quot;, prix=&quot; + prix +
&quot;, dureeDeVie=&quot; + dureeDeVie +
&#39;}&#39;;
}
}

huangapple
  • 本文由 发表于 2020年8月6日 20:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定