I have a simple Spring & Jersey application, which works perfectly well for consuming requests through a simple Resource. However, I'd like to return a JSON response - containing a simple JSON serialization of an object. To achieve this, I've added a maven dependency for jersey-json. As soon as I add this dependency, however, I get this error at server startup:
com.sun.jersey.api.container.ContainerException: No WebApplication provider is present at
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:69) at
com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:391)
I'm not totally clear upon exactly what a provider is, but I'm pretty certain that there should be a default one found.
For completeness, here's my Resource:
#Path("/scan")
#Resource
#Component
public class ScanResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/{barcode}")
public List<Scan> getScansForBarcode(#PathParam("barcode") Long barcode){
..snip..
return results;
}
}
A Scan object is a simple Entity Bean object.
The mvn dependency is:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.11</version>
</dependency>
Does anyone know why I might be getting the No WebApplication provider is present Exception? Any thoughts on how I might resolve it?
Thanks
You need to have jersey-server jar on your classpath as well. And you need to make sure that all your jars are from the same version, Jersey runtime won't be able to use provided classes otherwise.
Additionally (most likely not relevant here, but..) there is a recent change in module structure - servlet dependencies were separated to new modules. So if you are using servlets, you might want to depend on jersey-servlet (which depends on jersey-server).
I am also had this issue. The issue was resolved by having same version for "jersey-json" and "jersey-servlet"maven dependencies.
EX:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.13</version>
</dependency>
Related
I am trying to setup a ctakes application which gives a JSON output.
I have taken the ctakes module from https://codeload.github.com/apache/ctakes/zip/ctakes-4.0.0
I am trying to convert jcas object to json as below
CAS cas = jcas.getCas();
JsonCasSerializer jcs = new JsonCasSerializer();
jcs.setPrettyPrint(true);
StringWriter sw = new StringWriter();
jcs.serialize(cas, sw);
System.out.println(sw.toString());
But it seems in the downloaded project setup, there is no dependency for JsonCasSerializer. I have googled and found that we need to add uimaj-json jar in my project. I have added the same as external dependency (uimaj-json-3.1.1)- I have tried different versions as well. But I am getting multiple issues after adding the same jar into the project.
My understanding is that while adding the uimaj-json jar, my POM already contains the below dependency and the newly included uimaj-json jar has a lot of dependencies which is not in compatible with my current application uimj-core version.
<dependency>
<groupId>org.apache.uima</groupId>
<artifactId>uimaj-core</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.uima</groupId>
<artifactId>uimafit-core</artifactId>
<version>2.1.0</version>
</dependency>
Kindly help.
Do we have any sample ctakes project which contains jcas to json conversion?
Issue resolved!
It was a version mismatch between uimaj-core and uimaj-json. I was mixing the versions of uimaj-core and uimaj-json. You should use both of same version. I have used 2.8.1 for both.
Please refer:
https://www.mail-archive.com/dev#ctakes.apache.org/msg03498.html
I want to return JSON object back to the client side.I add dependencies and it is not working again.What is the purpose of 'repackaged'?I can not find any documentation of this pacakge to add in pom.xml.It allows me to create a JSONObject but in my console appear this error?!
pom.xml
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-labs</artifactId>
<version>${appengine.app.version}</version>
<scope>test</scope>
</dependency>
I used Gson and works for me.It is easy to implement and convert Java object to JSON and vice versa!Here is GSON dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Treat the 'repackaged' package as internal GAE implementation choices that may change. Such classes have their package path surgically altered to include repackaged so that they won't conflict with your choices.
I've had problems with Eclipse offering repackaged classes up as viable options. How to hide some Eclipse autocomplete results shows a way to fix that.
Just a follow up on this for IntelliJ users. You can tell the IDE to not offer repackaged classes in:
(for IntelliJ 2016.1)
Settings > Editor > General > Auto Import > Exclude from Import and
Completion
Enter there the following and you are done.
com.google.appengine.labs.repackaged
com.google.appengine.repackaged
A little background first.
I have an application that is deployed in Weblogic. It receives a Json response from a Service. I'm trying to use JsonPath to navigate the tree and I'm having an unusual issue.
I'm using Maven to build/deploy the application.
Dependency:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>json-path</artifactId>
<version>1.8.1</version>
</dependency>
After getting it running with the full response in Junit and realizing that it wasn't working in the application when deployed, I made it simpler and hard coded a very small subset of the data.
{
"ChangeStatus": {
"Code": {
"value": "1002"
},
"Description": {
"value": "Matched more then 10 records"
}
}
}
Here's what I'm looking at right now...
String miniJson = "{\"ChangeStatus\":{\"Code\":{\"value\":\"1002\"},\"Description\":{\"value\":\"Matched more then 10 records\"}}}";
JsonPath miniJsonPath = new JsonPath(miniJson);
String statusCode = miniJsonPath.getString("ChangeStatus.Code.value");
In JUnit, this code works and I can assert 1002 successfully.
In the application after pushing to weblogic, this exact code snippet does not work.
It throws a NoSuchMethodError.
Any ideas would be welcome.
FYI, we are on Weblogic 10.3.6
Thanks in advance!
I am not an expert in Weblogic, but as an alternative you can include
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.0-rc1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.0-rc1</version>
</dependency>
and create a class ChangeStatus with 2 members Code and Description and then you can deserialize the JSON using:
`new ObjectMapper().readValue(miniJson, ChangeStatus.class)`
Hope it hepls.
What I discovered is that jsonpath depends upon antlr.
Weblogic also includes this package, but I believe it is an older version.
I fixed the problem by telling Weblogic to use the classes included in the app.
weblogic.xml
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
First: can someone provide an explanation of the differences between org.json.simple.JSONObject and org.json.JSONObject?
Second: I have a code with org.json.JSONObject and org.json.JSONException. When I edit the code in eclipse ( JUNO) it resolves the type of JSONException and imports the package org.json.JSONException but when I run the project using maven in command line I have a problem ( JSONException cannot be resolved to a type). I tried to solve the issue by adding dependency to pom.xml like this :
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
but I am not sure if it is the right one . I even download the jar of org.
java-json.jar and add it to web deployment assembly but still the same error. Can anyone help ?
org.json and org.json.simple are two different Java libraries, which are incompatible with each other. The fact that they have the same name is only a misleading coincidence.
To compare the two libraries:
General comparison = http://www.rojotek.com/blog/2009/05/07/a-review-of-5-java-json-libraries/
org.json.simple = http://code.google.com/p/json-simple/
org.json = http://json.org/java
I am using RestEasy to marchal entities to JSON. That works okay but somehow every thing is represented as a String. e.g.
#XmlRootElement(name="TestObject")
public class TestObject {
private Long value;
public Long getValue(){
return value;
}
}
Instead of creating something like: {TestObject:{value:1234}}
It creates {TestObject:{value:"1234"}} (Please note the " " around the number)
So the long value is converted into a String. How can I avoid that?
I've asked on the Jackson forum which RestEasy is using for the JSON marchaling but they said it is probably caused by going Java->XML->JSON.
There doesn't seem to be a RestEasy forum and on the Seam forum no one could answer my question.
Does anyone else have the same problem?
Regards
Okay the problem is that RestEasy+Seam uses Jettison by default (and not Jackson).
Jettison does the marchaling via Java->XML->JSON.
The Jackson jars aren't actually included in the Seam distribution so you have to download RestEasy and copy all jars which mention jackson to your lib directory. When RestEasy finds the resteasy-jackson-provider.jar in the classpath, Jackson will be used instead of Jettison.
One problem I had when moving to Jackson from Jettison were cycling references. With Jettison you just annotate the method (e.g. a #ManyToOne relationship) with
#XmlTransient. For Jackson you have to annotate it with #JsonIgnore
use
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.0.1.GA</version>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
I use only resteasy-jettison-provider, but server hangs when I try to get JSON output, but after I excludes the jaxb-api and jaxb-impl. webservice works perfectly fine with JSON output