2

我需要 Wicket 来响应来自 AngularJS 的发布请求。

我像这样在检票口中设置了一个页面,但请求参数始终为空

@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
        }
    }
}

发送 POST 请求的 AngularJS 代码如下所示:

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

知道这里出了什么问题吗?谢谢!

4

3 回答 3

4

不确定这是否是最好的解决方案,但以下代码对我有用

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

            }

        }
    }
}

我遇到的另一个潜在解决方案是这个项目https://github.com/bitstorm/Wicket-rest-annotations

于 2013-08-14T18:29:20.807 回答
1

WicketStuff REST Annotations可以通过内置对 JSON 序列化/反序列化的支持来帮助实现这一目标。

于 2015-02-10T04:11:54.790 回答
1

我在我的一个项目中使用了Wicket 休息注解。这是它的 Maven 存储库。请在下面找到一个示例:

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>

创建 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) {
        ...
    }
}

在您的 Wicket WebApplication init() 方法中,注册其余资源:

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

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

REST Web 服务启动后,您可以发出 HTTP POST 请求:

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

而已。

于 2018-09-25T10:36:57.333 回答