[resolved] tRESTResponse returning 404 when no records found

One Star

[resolved] tRESTResponse returning 404 when no records found

Hi all, 
I want to have multiple tRestResponse component for each of my sujobs:  if a record exists in my db, I should have a 200 http status code, else, it should be  404.
T tried a little thing (please find attached the screenshots) but I'm always having a 404 http status code. 
thank you for your help
Cheers

Accepted Solutions
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

I'm afraid globalMap variables don't work like that. You either need to set them in Java code or using the tFlowToIterate or tSetGlobVar components. 
You can achieve your goal using the tFilterRow solution I suggested earlier. You simply need to set a boolean (or something else) in your tXMLMap to indicate the status of your FilmId query. Then pass that status out with the body Document. If the boolean is true, then filter it to go to the tRESTResponse with code 200, if it is false filter it to go to the tRESTResponse with code 404.
From memory, I believe you are sending the FilmId to the child job to be used in the tMySQLInput query. If you engineer that query so that it will always return a row regardless of whether the film is found or not, you can use the response in your tXMLMap to decide whether it is successful or not. Use that logic to set the boolean. Then, instead of having more than one output from the tXMLMap, have one output with the success/failure flag. Use that column with the tFilterRow component and then just map the filter/reject outputs to the appropriate tRESTResponse.

All Replies
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

*I tried to print the value of the ((Boolean)globalMap.get("FilmId")); it's always NULL !!
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

Basically, the data flow goes to both of the tRESTReponse components whether there is anything to output or not. You need to filter the flow.
Try using a tFilterRow to send the the data to the 200 or 404 output. You can filter on the boolean that you are filtering in the tMap at the moment. See below...
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

Well, if I filter on the boolean, I get a 500 server error on soap ui: 

if I filter on the "film_id" that I want to retrieve, I get always a 404 status code, 

and finally, if I remove the row "film_id" in the tfilterRow schema, I get always a 200 status code, even if the record doesn't exist in the db.
Regards
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

That is because the Boolean you are passing back is actually NULL. Your problem looks like it is centred on the ((Boolean)globalMap.get("FilmId")) .  How are you setting this value and in which Job (parent or child)? 
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

actually I thought it's a global variable that automatically stores the state of the excution of a component, for example, if my tRunJob  "FilmId" has been executed well, automatically ((Boolean)globalMap.get("FilmId)) would be true. So I haven't set that variable anywhere
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

The way I've been thinking the whole thing was wrong. The tests that I made, concern "whether the subjob was successfully completed or not", and it works if I see it that way. 
However, I don't want to test whether the job was successfull or not, I want to have  404 status error if the record I want to retrieve doesn't exist==> the job will be successfull but I'll have no results 
could you help me with this please
Thank you for your time
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

I'm afraid globalMap variables don't work like that. You either need to set them in Java code or using the tFlowToIterate or tSetGlobVar components. 
You can achieve your goal using the tFilterRow solution I suggested earlier. You simply need to set a boolean (or something else) in your tXMLMap to indicate the status of your FilmId query. Then pass that status out with the body Document. If the boolean is true, then filter it to go to the tRESTResponse with code 200, if it is false filter it to go to the tRESTResponse with code 404.
From memory, I believe you are sending the FilmId to the child job to be used in the tMySQLInput query. If you engineer that query so that it will always return a row regardless of whether the film is found or not, you can use the response in your tXMLMap to decide whether it is successful or not. Use that logic to set the boolean. Then, instead of having more than one output from the tXMLMap, have one output with the success/failure flag. Use that column with the tFilterRow component and then just map the filter/reject outputs to the appropriate tRESTResponse.
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

Okey, I'll work on it. 
I built the parent and the subjobs, but I can't access them from the browser; I get an "inaccessible web page", is that normal??!!
that wasn't the case when I had just one job.. 
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

That doesn't sound right, but I'm confused by the use of a browser?? If you meant you are having issues opening the Job in the Studio, just restart the Studio. It can be a bit quirky sometimes, especially if it has been left on for a while.
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

No, I mean that my Rest Service is no longer accessible via google Chrome using : "http://localhost:8184/getActors" 
When I execute my service via the studio, I can retrieve my data using the URL above, but when I  build them (the parent and the subjobs), all I get is: 
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

even in soap ui I can't access the service!!!
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

Ah, check that the service is actually running. Sometimes ports an become locked and you either need to change the port, restart your machine or just restart Talend. 
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

Oh, I restarted Karaf and TOS and it's perfectly working. :-)
thank you veery much ^^
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

Be careful if you are running the same service in your Karaf and your Studio at the same time. Ideally, you will only have it running in your Studio until you get these issues resolved, then put it in your Karaf to be tested there. Services tend to run slightly differently between the Studio and the Karaf as the Studio does not actually run its own Karaf, it emulates it. 
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

Thank you for those explanations. 
Concerning the error code, I was inspired by your hint; instead of creating and setting a global variable in my tXmlMap, I just created a new variable in the output, and mapped it with the "film_id", I recovered the hole schema in the parent job and filtered on that new variable in the tFilterRow. 

I would like to know if it's a proper way to behave. 
Cheers 
Sixteen Stars

Re: [resolved] tRESTResponse returning 404 when no records found

What you have done looks OK and if it does what you expect, it is behaving properly :-)
One Star

Re: [resolved] tRESTResponse returning 404 when no records found

Problem solved then Smiley Very Happy thank you