Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

spring boot - Custom Response for client in REST API in case of success or failed output

In my REST API in Spring Boot. I have made three methods, GET, POST & PUT. I want to find user details (which is saved in Person class) by giving userId and userSubId and display the result (either in case of success or failed) as response.

Also, I want to insert user details using POST method and display the result as response. And finally, I want to edit the existing details using PUT method by giving userId and userSubId and display the result (either success or failed) as response.

To display the results (either in case of success or failed), I have created a seaparate RESPONSE classs with 'message', 'details', 'status', 'timestamp' & 'errors' variables.

BUT, I want to customize my RESPONSE to be sent to client like this:

In case of Success, it should be like:

{
    "message": "Success",
    "details": [
        {
            "userId": 1,
            "userSubId": 2,
            "firstName": "John",
            "middleName": "Doe",
            "lastName": "Dean",
            "street": "Lincoln Street",
            "city": "Cetrump",
            "state": "Louis",
            "country": "Zaparta",
            "zipCode": "776655",
        }
    ],
    "status": "Ok",
    "timestamp": 2012-04-21T18:25:43-05:00,
    "errorCode": 200
}

In case of Failed, it should be like:

{
    "message": "Failed",
    "details": [
        {
            "userId": 2,
            "userSubId": 6
        }
    ],
    "status": "Not Found",
    "timestamp": 2013-04-21T18:25:43-05:00,
    "errorCode": 404
}

That is, the 'details' should be holding all the data in case of GET method and only few data (like userId and userSubId) in case of POST and PUT methods. And, I am not be able to do that. Please guide.


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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...