Tuesday, June 25, 2013

java.lang.NullPointerException at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)

Very small post, actually I got this error (java.lang.NullPointerException at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)) while hitting REST web service. I searched a lot and spent all most one day but could not get reason behind it.

Finally one got solution, so thought to share with you, it may be helpful some time.

Jersey uses the thread context class loader to load such classes. Perhaps you can do the following:

Thread .currentThread ().setContextClassLoader(this.getClass().getClassLoader()); 

Put the above line code, just before you hit the REST.

For Example:
Thread .currentThread ().setContextClassLoader(this.getClass().getClassLoader());
webResource = client.resource("your REST server URL");
client.addFilter(new HTTPBasicAuthFilter("serverUserName", "serverPassword"));

if (response.getStatus() != 200) {
   throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());

String output = response.getEntity(String.class);

If you some other solution, then please share.


Binod Suman