我正在使用泽西客户端来打电话给休息网络服务.
我的web服务正在使用json,所以我需要让json调用我的webservice提供者.
我是以下面的方式做的.
JSONObject object=new JSONObject();
object.put("name", employee.getName());
object.put("empId", employee.getEmpId());
object.put("organizationName", employee.getOrganizationName());
ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, object);
Run Code Online (Sandbox Code Playgroud)
但我得到以下例外:
09:52:01,625 ERROR [[mvc-dispatcher]] servlet mvc-dispatcher的Servlet.service()抛出异常 com.sun.jersey.api.client.ClientHandlerException:Java类型的消息体编写器,类net.sf. 在com.sun.jersey.client.url连接.URLConnectionClientHandler的com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)中找不到json.JSONObject和MIME媒体类型application/json. _invoke(URLConnectionClientHandler.java:204)位于com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)的com.sun.jersey.api.client.Client.handle(Client.java:648)在com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)的com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74)com.sun.jersey. api.client.WebResource $ Builder.post(WebResource.java:563)位于com.nec.jp.pflow.unc的com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44). controller.EmployeeController.addCus tomer(EmployeeController.java:29)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
但是,如果我将我的json转换为字符串表示形式,如:
String input = "{\"name\" : \"" + employee.getName() + "\",\"empId\" : \"" + employee.getEmpId() + "\",\"organizationName\" : \"" + employee.getOrganizationName() + "\"}";
ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, input);
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.
请建议如何在不获得上述异常的情况下放置我的JSON对象.什么是最好的方法?
提前致谢.
我得到了上述解决方案.现在我正在使用jackson-mapper api将POJO转换为json.
以下是代码段.
ObjectMapper mapper = new ObjectMapper();
ClientResponse response = …Run Code Online (Sandbox Code Playgroud) 我有一个问题.在我的应用程序中,我有一个servlet,其初始化代码如下所示.
public class GameInitServlet extends HttpServlet {
private static boolean initialized = false;
@Override
public void init() throws ServletException {
// This is a safeguard against running init() more than once.
synchronized (GameInitServlet.class) {
if (initialized) {
LOG.error("GameInitServlet has already been initialized... Bailing out!");
return;
}
initialized = true;
}
//some code here....
}
}
Run Code Online (Sandbox Code Playgroud)
注意:在web.xml中,上面的servlet在启动时加载为1,因此它将在启动应用程序时初始化.
所以我的问题是我们为什么要同步init方法.毕竟它将由servlet容器处理并且只被调用一次.我可以删除上述同步过程,或者删除后会对应用程序产生一些影响.
我遇到了一个单例类{lazy initialization}.代码如下
// Singleton reference for this class
private static volatile FileProperties INSTANCE = null;
public static FileProperties getInstance() {
if (INSTANCE == null) {
synchronized (FileProperties.class) {
if (INSTANCE == null) {
INSTANCE = new FileProperties();
}
}
}
return INSTANCE;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我们通过使INSTANCE变得不稳定而获得的好处 因为我们已经通过synchronized来处理线程安全问题.在这种情况下,volatile是否有任何好处?