Can anyone let me know, how message entity get separated from the HttpServletRequest object in RESTful service?
My understanding from Servlet programming is, whenever we will call doPost() method, here server thread will pass HttpServletRequest object from which we will access the header or data.
But in case of RESTful web service, whenever we will access any resource(method), it will just pass the message object(in form of string or mapped the passed JSON Object to class and pass corresponding class object to called method).
I don't know if it fully answers the question, but to extract the body from an HttpServletRequest, you can use either getInputStream or getReader, which provides you with a way to read the body of the HTTP request.
Hope this helps.
Related
I'm trying to leverage Postman's mock server feature to mock an API that my application calls.
This is a Post request. I have gone through the documentation and as advised I have saved the responses as examples.
When I try hit the mock URL I get the postman error response
Here is my setup -
My Collection with saved examples
MY mock server
After going through your query, I can see that you're trying to match an example based on the body passed with the request.
To match an example based on the request body, you can leverage the body matching feature of mock servers by:
Enabling the body matching feature from the mock edit page (Reference: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/setting-up-mock/#matching-request-body-and-headers).
OR
Passing an additional x-mock-match-request-body header with value as true along with your mock request to get the desired results.
You can find more information on how to use body matching feature with mock servers here: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/#6-check-for-header-and-body-matching.
Do let me know if this doesn't solve your issue. In that case, it would be helpful if you can share the mock request that you're sending to get the response.
Below is the sequence of events in a CRUD web service that I am trying to create
Step 1: User request POST /shape/trycreate (try-create request)
Step 2:Controller method receives object :tryCreate(Shape s)
Step 3: Service method returns matching shapes: Collection<Shape> tryCreate(Shape s)
Step 4: If duplicate shape exists throws some exception and ExceptionMapper returns failure response
If returned collection is then empty create the Shape and return SUCCESS response
else if return an object containing paths to view shapes that match the shape user was trying to create to a certain extent and return object also contains a continue path to still create a Shape
So basically response object contains paths like view/Shape/id001 view/Shape/id003 view/Shape/id007 which are Shapes like the Shape s which user was about to create and it also has a continue path create/shape/some-token
Here i think i can use some-token --> Shape object map on the server side where an entry lives for 5 minutes or so.
By using this i can validate that a user has not sent a direct request to create but it has gone through the step tryCreate->View Matching Shapes-> Still Create
Also it doesn't have to again send the details of Shape in create request which was already sent in tryCreate request.
Now the problem is that using a expiring cache on server side to store validated tokens isn't a Restful design.
Q1)**How should i ensure that this is not a direct request to create and it follows a trycreate request
**Q2) Do I resend the details of Shape in create request otherwise how do I achieve this?
Thanks
I believe the token approach you mentioned is Ok. The HTTP request is not stateful, its your business logic on the service side that does that. It is like creating an entity and then requesting for it, the two http request are not stateful, but if you have created the entity you will get it in the fetch request, otherwise not.
Can it be just about naming the endpoints, how about:
1) POST /shape/request
2) POST /shape/process
The 2nd is a valid RESTful request as described in this book, terming it as controller resource.
By the say, contrary to the heading, this explains the relation about REST and statelessness.
Being new to web development, I need some help in understanding what is the difference between the javax.servlet.http.Cookie and javax.ws.rs.core.Cookie.I assume that the latter can be used to set cookie into the response of a rest service. But can we also set the initial Cookie object into the HTTPServletResponse?
These are objects that represent the same underlying entity, namely an HTTP cookie as defined by the RFC. Both "do" the same thing, representing a cookie header in an HTTP response (a request cookie is a name=value pair only, whereas response cookies can have several additional attributes as described in the RFC). Where you use one vs the other is simply a matter of what you are coding. If you are writing a JAX-RS provider, then the JAX-RS apis will use javax.ws.core.Cookie. If you are writing an HttpServlet, then you use the javax.servlet.http.Cookie. JAX-RS implementations will also allow you to use context injection so that you can have direct access to the HttpServlet objects within your JAX-RS service provider
javax.servlet.http.Cookie is created and placed on the HTTP response object with the addCookie method.
Instead, the description for javax.ws.core.Cookie reads:
Represents the value of a HTTP cookie, transferred in a request
… so you'd expect the getCookies method on the HTTP request object to return an array of that type of cookies, but no, it returns an array of javax.servlet.http.Cookie. Apparently javax.ws.core.Cookie is used by some methods in the javax.ws.rs packages. So you use javax.ws.core.Cookie with jax-rs web services and javax.servlet.http.Cookie with HttpServlets and their request / response objects.
I'm planning to make an API (as a web service) for validating user input.
The API gets 3 parameters from a user as input, checks all the parameters are valid, then returns the result (ex: true or false) to the user.
And here's a rough sketch for the API (I doubt this is RESTful):
URL: http://my.domain.com/validate/v1 (POST)
Required parameter: param1, param2, param3
Result: To response body (XML/JSON) or response header (HTTP status)
But after googling API design and REST I found that something's wrong with this API design.
According to Wikipedia, Requests and responses are built around the transfer of representations of resources. But the API I'm making has nothing to do with resources. It doesn't CRUD any resources. All the API does is just taking inputs, validating them, and returning the result. And I'm stuck on designing the API with this requirement.
Any advices/corrections to this question are welcomed.
You are right that your problem better fits the RPC style, but it can be nevertheless easily mapped to REST. Here is how I would do it:
The POST method is frequently used in REST to create a new resource. The representation of this new resource is posted to an URL representing a collection of resources of the same type. If the operation is successful, the HTTP status code "201 Created" is returned with the representation in the repose body (essentially the same message body as the one sent in the post). The Content-Location header returned shows the URL assigned to the new resource. If the operation fails, it is signaled with a "400 Bad Request" status code and a more detailed human-readable error description in the message body.
As you can see, validation is already part of this common REST pattern. By what I understand, the only difference in your case is that you don't want to create (remember) this resource on your server. So don't. REST doesn't say you must. If you find it easier, imagine that the resource was indeed temporarily created but immediately deleted afterwards. Return the status code "200 OK" if the parameters pass validation and also return the parameters in the message body. Return "400 Bad Request" otherwise.
If the verb "validate" bothers you in the URL (it shouldn't), name the URL something else, perhaps something that would be an appropriate name for an object made up of the three parameters.
Hope this helps
Ferenc Mihaly
http://theamiableapi.com
I've created a ColdFusion Web Service, but it's returning WDDX instead of SOAP. How do I make it return SOAP instead of WDDX?
Have the <cffunction> return an XML object, and have the "returnformat" parameter be set to "plain".
<cffunction name="GetData" returntype="xml" returnformat="plain">
For complex objects, you need to setup the CFCs correctly. Read: Using ColdFusion components to define data types for web services
update: Or, you can create the XML representation of your object yourself with <cfxml>, then return the XML object with returnType="xml" in cffunction.
You may check out coldbox's XMLConverter Plugin as code sample for converting built-in CF complex types into XML.
A CFC method with access=remote ought to return soap, rather than WDDX. I'm sure I've used this functionality for years. What I'm suspecting may be happening is that the content-type is based on the request a client makes. I would download Soap-UI and test http://your.server/yourCFC.cfc?wsdl to see whether SOAP-UI gets WDDX thrown back at it. If is does, I'm at a bit of a loss, but do report it here anyway and I'll take a further look.
If Soap-UI sees a proper response, take a look at the headers it's sending and compare them to the request you're making (possibly through the browser?)
You can also use Fiddler to record soap-ui traffic and compare that against any other source of requests.
The http request thing above may be completely off, but it's relatively easy to check and I think it's ringing a bell.
You may also want to check the return type of the function you're writing. In order for CF to generate a good WSDL, it needs to be able to extract metadata from the CFC you're returning.
A bit late to the game but were you hitting it as a plain HTTP request and not as with a SOAP packet?
For example were you doing this:
http://api.example.com/something.cfc?method=test&arg1=val1
instead of an actual SOAP request with envelope, headers, body, etc?
The HTTP request returns WDDX by default or JSON by specifying the returnformat, while a SOAP request will return data in the format you are seeking.