我正在尝试执行POST请求/获取我想要发送一些值,在阅读后我发现@FormParam是我应该使用但我不能让它工作。我试着这样做我的Fetch
createOyster(){
var resource = "version=" + this.state.version + "&customer=" + this.state.customer + "&userId=" + this.state.userId + "&oysterName=" + this.state.oysterName + "&backupFile=" + this.state.backupFile;
console.log(resource)
fetch('http://localhost:8080/sleepy-oyster/v2/oyster/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify({
resource
})
})
}
console.log(资源)给出了
version=[object Object]&customer=NCC&userId=johjon&oysterName=NCC_johjon2&backupFile=Kund_NCC_Prod_2018-03-22.228680192.bak
当我尝试向我的REST请求时,看起来像这样
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createOyster(@FormParam("version") String version, @FormParam("customer") String customer,
@FormParam("userId") String userId, @FormParam("oysterName") String oysterName,
@FormParam("backupFile") String backupFile) throws IOException {
System.out.println(version + "HEJ HEJ HEJ HEJ HEJ HEJ HEJ");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
NexusResource resource = mapper.readValue(version, NexusResource.class);
System.out.println(resource);
dbWorker.restoreDatabase(userId, backupFile);
oysterWorker.createOyster(userId, oysterName);
oysterWorker.installOyster(oysterName);
String selectedVersion = versionWorker.getVersionFromNexus(resource,oysterName,customer);
return Response.ok(selectedVersion).build();
}
我得到一个NullPointerException,它没有指向任何让我困惑的代码
javax.servlet.ServletException: java.lang.NullPointerException
at org.glassfish.jersey.servlet.WebComponent.serviceImpl (WebComponent.java:489)
at org.glassfish.jersey.servlet.WebComponent.service (WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:388)
自从我第一次使用@FormParam以来,我可能搞砸了一些东西。任何人都可以看到为什么它给出null。我的REST中的Sout也提供了null,因此REST无法获得它。我想这是因为版本是[object Object],因为其余的不给出null。我该怎么处理?
如果我只是console.log(this.state.version),我会得到我的完整对象
{resourceURI: "http://nexus.ad.hrm.se/nexus/service/local/reposit…ent/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/", relativePath: "/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/", text: "16.1-SNAPSHOT", leaf: false, lastModified: "2018-05-02 06:57:07.0 UTC", …}