Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

java - HTTP request always add error and warning in response body

I am working on a java backend that is accessible via rest api. For example there is a resource to create and update a product. Depending on the user the products can have different properties which i handled in a dynamic way using a hashmap (so which products has which properties are defined somewhere else). Properties could be name, source, supplier, amount, etc.

Sample:

public class Product
    int id;
    Set<ProductProperty>;

The products should be updated via REST API with sending a JSON in the body:

{productId: 1
supplier: XY
amount: 23
productCode: ZZ}

The problem I have is that a specific property could be deleted for a product. Of course all other properties should be updated accordingly. And for the failed properties i would like to return the error to the requester. So my idea is to return a 200 respone which has a JSON object in the response body like:

{[errors: 
    {
        "productCode": "ZZ",
        "errorText": "Code lenght is less than 12 digits",
    },
    {
        "foobarProperty": "123124124",
        "errorText": "Property doest not exist for this product",
    }
]}

With that i could show the errors in the UI (will be setup with react later) so the user knows which properties could not be updated and why. So i wanted to know if this is a good approach or if there are best practice i dont know yet.

If i think more, lets say the product should be also returned would it be ok to set the product as a json including the error part like:

       {
            productId: 1 
            supplier: XY
            amount: 23
            productCode: YY}, // has the updated prop
       },        
       {
            [errors: 
            {
                "productCode": "ZZ",
                "errorText": "Code lenght is less than 12 digits",
            },
            {
                "foobarProperty": "123124124",
                "errorText": "Property doest not exist for this product",
            }
       ]}
question from:https://stackoverflow.com/questions/66052692/http-request-always-add-error-and-warning-in-response-body

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In my opinion, you should not mix both the things together. You should keep it simple, either the request failed or succeeded. Mapping with HTTP error codes become more intuitive that way. Also, you should consider the fact that maybe user wanted all the properties to be changed together and taking assumptions that it is okay to update a few and pass others in some sort of an “error” message can be dangerous.

Another aspect that you should think about is the usability of your REST APIs. What if your client invokes your APIs not via your UI but let’s say some CLI tool such as curl? Posing as SUCCESS (200), when actually it is partially successful can lead clients to have wrong impressions that there was no problem in their request.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...