可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using jersey client for making call to rest webservice.
My webservice is consuming the json, so i need to put json in making call to my webservice provider.
I am doing it in below way.
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);
but i am getting the below exception:
09:52:01,625 ERROR [[mvc-dispatcher]] Servlet.service() for servlet
mvc-dispatcher threw exception
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class net.sf.json.JSONObject, and MIME media
type, application/json, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
at com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44)
at com.nec.jp.pflow.unc.controller.EmployeeController.addCustomer(EmployeeController.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
But if i convert my json to string representation like :
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);
then it is working fine.
Please suggest how can i put my JSON Object without getting the above exception. What is the best way?
Thanks in advance.
I got a solution for the above. Now I am making use of jackson-mapper api for converting the POJO to json.
Below is the code snippet.
ObjectMapper mapper = new ObjectMapper();
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, mapper.writeValueAsString(employee));
回答1:
A better way is to tell jersey-client
that it may use jackson
for serialization:
https://stackoverflow.com/a/2580315/516188
Pasting the setup:
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJsonProvider.class);
Client clientWithJacksonSerializer = Client.create(cc);
JacksonJsonProvider
is in the jackson-jaxrs-json-provider
package.
回答2:
Just copying the solution from the question above, just in case someone is looking for answer.
Use jackson-mapper api for converting the POJO to json as shown in the code snippet below:
ObjectMapper mapper = new ObjectMapper();
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, mapper.writeValueAsString(employee));
回答3:
adding a .toString() worked for me:
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.toString());
回答4:
JSONObject object=new JSONObject();
need to provide @XMLrootElement
Annotation....if you are accessing the web service from remote....i.e XML Mapping..for Model class.
回答5:
After lot of reading here is the solution which i arrived at :
If you are using JERSEY-CLIENT as specified as below of version 1.9 or higher
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
Then use the MOXY Jar as specified below of version 2.19 or higher
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.19</version>
</dependency>
And then add the MOXyJsonProvider.class in the Class of Client configuration as shown below :
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MOXyJsonProvider.class);
Client client = Client.create(cc);
Now make the required configuration to Client Object to connect to server and execute the request:
WebResource webResource = client.resource(*restFulPath*);
Builder builder = webResource.accept(MediaType.APPLICATION_JSON);
builder.header("Content-Type", "application/json");
builder.header("Accept ", "application/json");
builder.accept(MediaType.APPLICATION_JSON);
try
{
String requestData = "*requestInJson*";
ClientResponse response = null;
response = builder.post(ClientResponse.class, requestData);
if (response.getStatus() == 200)
{
MappedJsonReportOut res = response.getEntity(MappedJsonReportOut.class);
}
}
回答6:
You can also use the entity() methods to achieve this, as follows:
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.entity(employee, MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class);
or
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(employee)
.post(ClientResponse.class);
回答7:
Error Which m getting is little Bit Different But Finally Solution is Work for Me
Caused by: com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.siemens.spring.model.User, and MIME media type, application/json, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
at com.siemens.controller.UserRegistration.process(UserRegistration.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
And This is Solution Worked for Me :
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/NewsTickerServices/Siemens/CreateJUser");
// value added
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class,mapper.writeValueAsString(user));//user is object of User Pojo class