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!
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
WicketStuff REST注释可以通过对JSON序列化/反序列化的内置支持来帮助完成此任务。
I used Wicket rest annotaions in one of my projects. Here is its the Maven repository . Please find below an example:
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:
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:
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:
POST URL: http://localhost:8080/api/my/rest/url
POST data: {json data for SomeObject}
Content-Type: application/json
That's it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.