Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    RESTHandler javadoc

    Would be good to include the defaultDataFormat init parameter - eg, if you want to return JSON instead of XML.

    An example of a web.xml config usage would be nice too:

    Code:
    	<servlet>
    		<servlet-name>RESTHandler</servlet-name>
    		<servlet-class>com.isomorphic.servlet.RESTHandler</servlet-class>
    		<init-param>
    			<param-name>defaultDataFormat</param-name>
    			<param-value>json</param-value>
    		</init-param>
    		<init-param>
    			<param-name>wrapJSONResponses</param-name>
    			<param-value>false</param-value>
    		</init-param>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>RESTHandler</servlet-name>
    		<url-pattern>/restapi/</url-pattern>
    	</servlet-mapping>

    #2
    The JSON returned is broken.

    Code:
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCache:false,status:0,startRow:0,data:[{date:null,name:"booo",testId:174,value:23.0}]}}[
    stuff it into http://www.jslint.com/ and you get

    Code:
    Error:
    
    Problem at line 1 character 2: Expected a string and instead saw response.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 12: Expected a string and instead saw endRow.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 21: Expected a string and instead saw queueStatus.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 35: Expected a string and instead saw totalRows.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 47: Expected a string and instead saw isDSResponse.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 65: Expected a string and instead saw invalidateCache.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 87: Expected a string and instead saw status.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 96: Expected a string and instead saw startRow.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 107: Expected a string and instead saw data.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 114: Expected a string and instead saw date.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 124: Expected a string and instead saw name.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 136: Expected a string and instead saw testId.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    Problem at line 1 character 147: Expected a string and instead saw value.
    
    {response:{endRow:1,queueStatus:0,totalRows:1,isDSResponse:true,invalidateCac...
    
    JSON: bad.

    Comment


      #3
      because the key names should be wrapped in quotes.

      Comment


        #4
        Ok, I tried to use XML instead of JSON. Which is also does not work, because the Content-Type is "text/html".

        How do you set the Content-Type?

        I've tried pretty much all the obvious things, like overloading doGet and setting the type, and started getting into non-obvious, eg. decompiling RESTHandler and trying to figure out how it is set.

        No luck.

        Somewhere in RPCManager.send() the Content-Type is set to "text/html" and I've no idea how to override it.

        ....help!

        Comment


          #5
          Well, after several painful hours trying to get RESTHandler to behave I ended up writing my own RestHandler.

          I still managed to use my SmartGWT DS definitions (yay!). The primary thing I needed (ie, request to document/expose) was RestRequestParser.parseRequest which sets up the DSRequest in a way that it can be executed by the SmartGWTEE framework.

          Perhaps you can point me to other resources that describe the server side setup of a DSRequest? It is non-trivial and there is quite a bit of mapping done to convert a HttpRequest into a DSRequest.

          It seems that the suggested way of doing this is through an RPCManager. My issue with the RPCManager was primarily the use of RPCManager.send() (which - as stated above, insisted on writing out text/html) - perhaps if I have time, I will go back to this and look at extending RPCManager and overriding send() instead.

          Comment


            #6
            Not sure if SmartGWT has this exposed but in SmartClient you can set the content type on the DSRequest.

            dsRequest.contentType = 'application/json'

            Comment


              #7
              Ah, I'll give that a shot. I tried many different API methods, but I don't think I tried to modify the DSRequest.

              Comment


                #8
                We've gone ahead and fixed the content type problem already, as well as made the JSON strictly conformant (what was being delivered before worked for a true JavaScript eval() and was more efficient for that use case, but was not technically JSON-compliant).

                Comment


                  #9
                  Great. Thanks.

                  Comment


                    #10
                    Is there any documentation/resource I might look at to get a feel for what writing my own RestHandler would take?

                    e.g., is RestRequestParser.parseRequest documented?

                    (this in the context of http://forums.smartclient.com/showthread.php?t=18256)

                    Comment


                      #11
                      Not really (most classes don't discuss possible replacement implementations!), but you basically need come up with a multi-operation format, create client code that can put it together, then server code that can parse, all while preserving types including nuances like date vs datetime vs time, and getting timezones right so that offsets aren't inadvertently introduced.

                      From there, there are docs on how to use the DataSource APIs standalone, in other words, without servlets, or from a command line application.

                      Comment


                        #12
                        i ended up using resteasy to make my REST API. it's a pretty great tool.

                        Comment

                        Working...
                        X