简体   繁体   English

使用Wicket作为REST API

[英]Use Wicket as a REST API

I need Wicket to respond to a post request from AngularJS. 我需要Wicket来响应AngularJS的发布请求。

I set up a page in wicket like this but the request parameters are always empty 我在wicket中设置了这样的页面,但请求参数始终为空

@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: 发送POST请求的AngularJS代码如下所示:

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

Any idea what's going wrong here? 知道这里出了什么问题吗? Thanks! 谢谢!

Not sure if this is the best solution but the following code is working for me 不知道这是否是最好的解决方案,但是以下代码对我有用

@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) {

            }

        }
    }
}

Another potential solution I came across was this project https://github.com/bitstorm/Wicket-rest-annotations 我遇到的另一个潜在解决方案是该项目https://github.com/bitstorm/Wicket-rest-annotations

WicketStuff REST注释可以通过对JSON序列化/反序列化的内置支持来帮助完成此任务。

I used Wicket rest annotaions in one of my projects. 我在我的一个项目中使用了Wicket rest注释 Here is its the Maven repository . 这是它的Maven存储库 Please find below an example: 请在下面找到一个例子:

pom.xml 的pom.xml

<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-restannotations</artifactId>
    <version>8.1.0</version>
</dependency>
<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-restannotations-json</artifactId>
    <version>8.1.0</version>
</dependency>

Create your Wicket REST resource: 创建您的Wicket REST资源:

import org.wicketstuff.rest.annotations.MethodMapping;
import org.wicketstuff.rest.annotations.parameters.RequestBody;
import org.wicketstuff.rest.resource.gson.GsonRestResource;
import org.wicketstuff.rest.utils.http.HttpMethod;

public class MyPostHandler extends GsonRestResource {    
    @MethodMapping(value = "/my/rest/url", httpMethod = HttpMethod.POST)
    public boolean handlePost(@RequestBody SomeObject someObject) {
        ...
    }
}

And in your Wicket WebApplication init() method, register the rest resource: 然后在您的Wicket WebApplication init()方法中,注册其余资源:

    mountResource("/api", new ResourceReference("restReference") {
        MyPostHandler resource = new MyPostHandler();

        @Override
        public IResource getResource() {
            return resource;
        }
    });

Once the REST web service is up running, you can issue HTTP POST request: REST Web服务启动后,您可以发出HTTP POST请求:

POST URL: http://localhost:8080/api/my/rest/url
POST data: {json data for SomeObject}
Content-Type: application/json

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM