尝试使用Jackson序列化一个非常简单的对象时,我得到了一个例外.错误:
org.codehaus.jackson.map.JsonMappingException:没有为类MyPackage.TestA找到序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))
下面是序列化的简单类和代码.
任何人都可以告诉我为什么会收到此错误?
public class TestA {
String SomeString = "asd";
}
TestA testA = new TestA();
ObjectMapper om = new ObjectMapper();
try {
String testAString = om.writeValueAsString(testA); // error here!
TestA newTestA = om.readValue(testAString, TestA.class);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用这个方法的RESTfull webservice:
@POST
@Consumes({"application/json"})
@Path("create/")
public void create(String str1, String str2){
System.out.println("value 1 = " + str1);
System.out.println("value 2 = " + str2);
}
Run Code Online (Sandbox Code Playgroud)
在我的Android应用中,我想调用此方法.如何使用org.apache.http.client.methods.HttpPost为参数赋予正确的值;
我注意到我可以使用注释@HeaderParam并简单地将标头添加到HttpPost对象.这是正确的方法吗?这样做:
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("str1", "a value");
httpPost.setHeader("str2", "another value");
Run Code Online (Sandbox Code Playgroud)
在httpPost上使用setEntity方法将不起作用.它仅使用json字符串设置参数str1.使用时如:
JSONObject json = new JSONObject();
json.put("str1", "a value");
json.put("str2", "another value");
HttpEntity e = new StringEntity(json.toString());
httpPost.setEntity(e);
//server output: value 1 = {"str1":"a value","str2":"another value"}
Run Code Online (Sandbox Code Playgroud) 我如何在我的webservice中使用json参数,我可以使用@PathParam获取参数但是获取json数据作为参数不知道该怎么做.
@GET
@Path("/GetHrMsg/json_data")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public String gethrmessage(@PathParam("emp_id") String empid) {
Run Code Online (Sandbox Code Playgroud)
}
用什么代替@PathParam以及以后如何解析它.
我有两个数组参数和对象数组,我想传递它们,我可以使用这样的方法
@POST
@Path("Test3")
@Produces("text/plain")
@Consumes({"application/json"})
public String Test3(String[] id1,String[] id2 ,Object [] oo) {
String result = "Hello ";
....
....
return result;
}
Run Code Online (Sandbox Code Playgroud)
什么是我应该传递给这个方法的相应的json
我尝试了很多jsons,我总是得到这样的错误
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition 4.0 - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Internal Server Error</h1><hr/><p><b>type</b> Exception report</p><p><b>message</b>Internal Server Error</p><p><b>description</b>The server encountered an internal error that prevented it …Run Code Online (Sandbox Code Playgroud)