I have a Jersey based Rest WS which outputs JSON. I am implementing a Jersey Client to invoke the WS and consume the JSON response. The client code I have is below
I was able to fix the issue by adding the maven dependency for jersey-json.
In my case, I'm using POJO. And I forgot configure POJOMappingFeature as true. Maycon has pointed it out in an early answer. However some guys might have trouble to configure it in web.xml correctly, here is my example.
<servlet>
<servlet-name>Jersey Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Mine was similar situation. I get this error in crucial times and I did not remember how I had solved it before, which I did many number of times. After strenuous hours of inspection, I have solved and reproduced the error and made sure how simple is the solution for this situation.
Okay - solution : remove (or correct) any newly made changes to your main project's properties files.
Yes, this is it. In fact, my project is a multi-moduled huge one, and you know going without correct dependencies is a rare case scenario as is fetched often from git (RAC too). My project was getting almost all configurable things from a properties file that is common to about 15 or so modules (sub projects) with good amount of intra-dependencies among them. jersey-json's dependency is always there in my merged parent pom. XML Annotations aren't a problem as the project is being run for around 100 times after modifying them. some solutions here point to web.xml and things like POJOMappingFeature. In my case, I haven't even touched the webapp module in this build. so anyhow, this solution worked for me and I am spending time to record this in SO in case if I ever happen to fall into this error, I would not have to waste my sleepy nights. (of course, for you too)
for Python and Swagger example:
import requests
base_url = 'https://petstore.swagger.io/v2'
def store_order(uid):
api_url = f"{base_url}/store/order"
api_data = {
'id':uid,
"petId": 0,
"quantity": 0,
"shipDate": "2020-04-08T07:56:05.832Z",
"status": "placed",
"complete": "true"
}
# is a kind of magic..
r = requests.post(api_url, json=api_data)
return r
print(store_order(0).content)
Most important string with MIME type: r = requests.post(api_url, json=api_data)
Just add below lines in your POJO before start of class ,and your issue is resolved. @Produces("application/json") @XmlRootElement See example import javax.ws.rs.Produces; import javax.xml.bind.annotation.XmlRootElement;
/**
* @author manoj.kumar
* @email kumarmanoj.mtech@gmail.com
*/
@Produces("application/json")
@XmlRootElement
public class User {
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
add below lines inside of your web.xml
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
Now recompile your webservice everything would work!!!
We also decided to use jersey as a solution. But as we are using org.JSON in most of the cases this dependency is unecessary and we felt not good.
Therefore we used the String representation to get a org.JSON object, instead of
JSONObject output = response.getEntity(JSONObject.class);
we use it this way now:
JSONObject output = new JSONObject(response.getEntity(String.class));
where JSONObject comes from org.JSON and the imports could be changed from:
-import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;