You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by robianmcd <ro...@gmail.com> on 2013/08/14 19:26:54 UTC

Use Wicket as a REST API

I need Wicket to respond to a post request from AngularJS. 

I set up a page in wicket like this but the request parameters are always
empty

    @MountPath(value = "/api/my/rest/url")
    public class MyPostHandler extends SecureWebPage {

        public MyPostHandler () {
            final WebRequest webRequest = (WebRequest) getRequest();
            final HttpServletRequest rawRequest = (HttpServletRequest)
webRequest.getContainerRequest();

            if (rawRequest.getMethod().equalsIgnoreCase("POST")) {
                webRequest.getRequestParameters().getParameterNames();
//Returns an empty list
                webRequest.getPostParameters().getParameterNames();
//Returns an empty list
            }
        }
    }

The AngularJS code that is sending the POST request looks like this:

    $http.post('/api/my/rest/url', {some:"data", other:"stuff"});

Any idea what's going wrong here?
Thanks!



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Use Wicket as a REST API

Posted by robianmcd <ro...@gmail.com>.
Yeah I was using this function to send JSON from a page.

protected void setJsonResponse(final String jsonString, final Duration
cacheDuration) {
        getRequestCycle().scheduleRequestHandlerAfterCurrent(new
IRequestHandler() {

            @Override
            public void respond(IRequestCycle requestCycle) {
                AbstractResourceStreamWriter rstream = new
AbstractResourceStreamWriter() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void write(OutputStream output) throws
IOException {
                        output.write(jsonString.getBytes());
                    }

                    @Override
                    public String getContentType() {
                        return MimeType.JSON.toString();
                    }

                };

                ResourceStreamRequestHandler handler = new
ResourceStreamRequestHandler(rstream);
                handler.setContentDisposition(ContentDisposition.INLINE);
                handler.setCacheDuration(cacheDuration);
               
getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
            }

            @Override
            public void detach(IRequestCycle requestCycle) {
                // Do nothing here, move along...
            }
        });
    }

Maybe resources have a better way of doing it though so I'll try doing it
with a resource.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660921.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Use Wicket as a REST API

Posted by Martin Grigorov <mg...@apache.org>.
I guess you expect JSON response for Angular's needs.
Pages usually produce HTML response.


On Thu, Aug 15, 2013 at 5:43 PM, robianmcd <ro...@gmail.com> wrote:

> What would the advantage be of using a resource instead of a page? I'll
> take
> a look a wicket-rest-annotation.
>
> Thanks
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660919.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Use Wicket as a REST API

Posted by robianmcd <ro...@gmail.com>.
What would the advantage be of using a resource instead of a page? I'll take
a look a wicket-rest-annotation.

Thanks



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660919.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Use Wicket as a REST API

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

I'd definitely recommend using IResource for something like this.

Also check https://github.com/bitstorm/Wicket-rest-annotations. It will be
merged to WicketStuff soon.


On Thu, Aug 15, 2013 at 1:03 AM, Bas Gooren <ba...@iswd.nl> wrote:

> Hi,
>
> Yes, what you are doing is a perfectly ok way to handle this.
> Optionally you could use a shared resource for this, but I say that's just
> a matter of preference.
>
> We use something inspired by https://code.google.com/p/**wicket-rest/<https://code.google.com/p/wicket-rest/>
> This gives you some abstraction over the json (or xml/...) handling.
> Furthermore you can override sensible methods like "doGet" or "doPost"
> which clearly communicate intent.
>
> Met vriendelijke groet,
> Kind regards,
>
> Bas Gooren
>
> Op 14-8-2013 20:32, schreef robianmcd:
>
>  I found that this works but I'm not sure if there is a better way of doing
>> this:
>>
>> @MountPath(value = "/api/my/rest/url")
>> public class MyPostHandler extends SecureWebPage {
>>
>> public MyPostHandler () {
>>      final WebRequest webRequest = (WebRequest) getRequest();
>>      final HttpServletRequest rawRequest = (HttpServletRequest)
>> webRequest.**getContainerRequest();
>>
>>      if (rawRequest.getMethod().**equalsIgnoreCase("POST")) {
>>
>>          BufferedReader br;
>>                try {
>>                    br = rawRequest.getReader();
>>                    String jsonString = br.readLine();
>>                    //Do something with the JSON here
>>                }
>>                catch (IOException e) {
>>
>>                }
>>
>>      }
>> }
>> }
>>
>>
>>
>> --
>> View this message in context: http://apache-wicket.1842946.**
>> n4.nabble.com/Use-Wicket-as-a-**REST-API-tp4660894p4660898.**html<http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660898.html>
>> Sent from the Users forum mailing list archive at Nabble.com.
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: Use Wicket as a REST API

Posted by Bas Gooren <ba...@iswd.nl>.
Hi,

Yes, what you are doing is a perfectly ok way to handle this.
Optionally you could use a shared resource for this, but I say that's 
just a matter of preference.

We use something inspired by https://code.google.com/p/wicket-rest/
This gives you some abstraction over the json (or xml/...) handling.
Furthermore you can override sensible methods like "doGet" or "doPost" 
which clearly communicate intent.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 14-8-2013 20:32, schreef robianmcd:
> I found that this works but I'm not sure if there is a better way of doing
> this:
>
> @MountPath(value = "/api/my/rest/url")
> public class MyPostHandler extends SecureWebPage {
>
> public MyPostHandler () {
>      final WebRequest webRequest = (WebRequest) getRequest();
>      final HttpServletRequest rawRequest = (HttpServletRequest)
> webRequest.getContainerRequest();
>
>      if (rawRequest.getMethod().equalsIgnoreCase("POST")) {
>
>          BufferedReader br;
>                try {
>                    br = rawRequest.getReader();
>                    String jsonString = br.readLine();
>                    //Do something with the JSON here
>                }
>                catch (IOException e) {
>
>                }
>
>      }
> }
> }
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660898.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


Re: Use Wicket as a REST API

Posted by robianmcd <ro...@gmail.com>.
I found that this works but I'm not sure if there is a better way of doing
this:

@MountPath(value = "/api/my/rest/url")
public class MyPostHandler extends SecureWebPage {

public MyPostHandler () {
    final WebRequest webRequest = (WebRequest) getRequest();
    final HttpServletRequest rawRequest = (HttpServletRequest)
webRequest.getContainerRequest();

    if (rawRequest.getMethod().equalsIgnoreCase("POST")) {

        BufferedReader br;
              try {
                  br = rawRequest.getReader();
                  String jsonString = br.readLine();
                  //Do something with the JSON here
              }
              catch (IOException e) {

              }

    }
}
}



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Use-Wicket-as-a-REST-API-tp4660894p4660898.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org