質問

If I PUT a JSON object to my restlet, eg.

curl -X PUT -H "Content-Type: application/json" -d '{"email":"foo@baar"}' http://localhost:8888/api/exitems/5629499534213120

how do I retrieve the original JSON, ie. the {"email":"foo@baar"} portion within my restlet?

UPDATE

Here is the background to the question ...

I have a Controller class, extending ServerResource, that handles REST CRUD operations on one of my model POJOs (Person). In the update() (ie. PUT) method, I am being passed an instantiated Person object, but what I don't know is which fields were PUT by my REST client.

  • If the client PUT excluded a property (say) familyName - intending this to mean 'do not update familyName', it is null in the POJO.

  • On the other hand, if the client PUT a null value for familyName - intending this to mean 'set familyName to null', then the POJO familyName is also null.

How do I distinguish between these two scenarios?

役に立ちましたか?

解決

As you are extending ServerResource, you can either call getRequestEntity() to retrieve the representation, or declare a Representation parameter on your @Post annotated Java method.

UPDATE

Looking at your updated question, it seems that you don't use the PUT method properly. The entity that you pass with your PUT request should contain the complete new state for your target resource.

In your case, it seems that you want to do only partial updates. You should rather consider using PATCH method if you really need such level of optimization.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top