我正在尝试向我的服务方法发送请求
OperationResult makeOperation(
@WebParam(name = "ident") @PathParam("ident") String ident,
@WebParam(name = "templateId") @PathParam("templateId") Long templateId,
@WebParam(name = "paySystemId") @QueryParam("paySystemId") Integer paySystemId,
@WebParam(name = "currency") @QueryParam("currency") Integer currency,
@WebParam(name = "payAmount") @QueryParam("payAmount") Long payAmount,
@WebParam(name = "payerPhoneNumber") @QueryParam("payerPhoneNumber") String payerPhoneNumber,
@WebParam(name = "additionalParameters") AdditionalParameters parameters) throws FailureException;
当我创建body并将json设置为{"paySystemId":12312}
时,我看到异常:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "paySystemId" (class com.fib.paynet.classes.TemplateParameters), not marked as ignorable
我无法理解为什么杰克逊不喜欢这个我的要求......
答案 0 :(得分:2)
您的JSON不正确。
这将是格式正确的JSON:
{ "paySystemId" : 12312 }
如果情况并非如此,这里有一个快速构建的客户端/服务器似乎对我有用(如果可能,请使用我的客户端测试):
<强> MainClient.java 强>
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class MainClient {
public static void main(String[] args) {
new MainClient().start();
}
private void start() {
WebResource webResource = Client.create().resource("http://localhost:8080/rest/test/");
String param = "{ \"paySystemId\" : 12312 }";
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, param);
readResponse(response);
}
private void readResponse(ClientResponse response) {
int status;
status = response.getStatus();
String body;
body = response.getEntity(String.class);
if (Status.fromStatusCode(status) == Status.OK) {
System.out.println("BEGIN OBJECT OUTPUT --");
System.out.println(body);
System.out.println("END OBJECT OUTPUT --");
} else {
try {
JSONObject jsonStack = new JSONObject(body);
JSONObject jsonResponse = new JSONObject(jsonStack.get("response").toString());
System.out.println("BEGIN ERROR --");
System.out.println(jsonResponse.toString());
System.out.println("END ERROR --");
} catch (Exception e) {
System.out.println("NON-JSON RESPONSE BODY BEGIN --");
System.out.println(body);
System.out.println("NON-JSON RESPONSE BODY END -- \n");
e.printStackTrace();
}
}
}
}
<强> MainServer.java 强>
import java.net.InetSocketAddress;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;
public class MainServer {
public static void main(String[] args) throws Exception {
new MainServer().start();
}
private void start() throws Exception {
ServletHolder holder = new ServletHolder(new ServletContainer());
holder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
holder.setInitParameter("com.sun.jersey.config.property.packages", "stack.teste");
holder.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
holder.setInitOrder(1);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setDisplayName("Simple RESTful Jetty Server");
context.setContextPath("/rest");
InetSocketAddress address = new InetSocketAddress("localhost", 8080);
Server server = new Server(address);
server.setHandler(context);
context.addServlet(holder, "/*");
server.start();
}
}
<强> Service.Java 强>
import javax.jws.WebParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public class Service {
@POST
public Response getEntidade(@WebParam(name = "paySystemId") @QueryParam("paySystemId") Integer paySystemId,
@WebParam(name = "currency") @QueryParam("currency") Integer currency) {
System.out.println(paySystemId);
return Response.ok("{\"status\" : 1}").build();
}
}
答案 1 :(得分:0)
paySystemId指定为@QueryParam,因此应该在URL的查询字符串中传递,而不是在请求正文中作为json in传递。