some problems parsing xml from webservice with JAXB - java

I am having issues with parsing the xml response got from the service at http://wiki.dbpedia.org/Lookup
My code for the main is the one up here, toghether with annotated beans that build up the xml.
I'd like to 'debug' what's going on in the JAXBContext, so that I can see what I messed up in the annotated beans. The only thing I found it is possible is to register an EventHandler like this:
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
that prints errors like these:
uri http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=galway&MaxHits=5
DefaultValidationEventHandler: [ERROR]: unexpected element (uri:"http://lookup.dbpedia.org/", local:"Result"). Expected elements are <{}Result>
Location: line 3
It seems there is an unexpected element Result, but I can't manage to fix it.
Can someone guide me in understanding the JAXB errors more in depth? I really can't figure out what the errors really mean (as I already have set up namespace = "http://wiki.dbpedia.org/Lookup" in the ArrayOfResult class).

You have the namespace information specified on ArrayOfResult but not on Result:
package it.cybion.dbpedia.textsearch.rest.response;
import java.net.URI;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Result", namespace="http://lookup.dbpedia.org/")
#XmlAccessorType(XmlAccessType.FIELD)
public class Result {
}

Related

How to access AEM Core models in a Sling model

Specifically, I am trying to enable .SVG files to be usable by the core image component.
Right now I am making a sling model that ideally I would like to access the returned values of the getSrc() and getFileReference() classes in the core AEM Component interface located here.
I am very new to AEM development and Sling models. Am I missing some vital functionality that would let me do this?
Here is my code, which probably isn't at all helpful at this point.
package com.site.core.models;
import com.adobe.cq.wcm.core.components.models.Image;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.*;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.apache.sling.settings.SlingSettingsService;
import javax.jcr.Node;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
#Model(adaptables = SlingHttpServletRequest.class)
public class ImageModel {
private String src = Image.getSrc();
return src;
}
As I mentioned in my comment, the link you are referring to is an interface, the implementation of that interface is here
In order to use your own implementation, you have two options:
In the image component html, change the data-sly-use to refer to your impl: com.site.core.models.ImageModel
Create a separate model that implements the Image interface and give it a high ranking to be picked up instead of the existing impl.
Disclaimer: I have not tested #2 but the documentation suggests that it's possible.

Can't resolve symbol toJson

I'm using json4s in a play project, and I'm also using a library called sbt-buildinfo which generates Scala source from your build definitions.
Now, in the sbt-buildinfo library the say you need to add some line of code: buildInfoOptions += BuildInfoOption.ToJson so you can use .toJson, but from some reason I can use .toJson.
this is how I do it:
import _root_.util.{AuthenticatedAction}
import buildinfo.BuildInfo
import com.google.inject.Inject
import org.json4s.BuildInfo
import play.api._
import play.api.mvc._
class AppInfo #Inject()(implicit configuration: Configuration) extends Controller {
def appVerion = AuthenticatedAction {
Ok(BuildInfo.toJson)
}
but the import buildinfo.BuildInfo stays gray....so it looks like I'm not using it. I refreshed the build.sbt and all, what could it be?
You have multiple imports to a BuildInfo object. org.json4s.BuildInfo will probably shadow your buildInfo.BuildInfo import and therefore, it does not have the required member. Try writing out the entire package name that you need:
Ok(buildinfo.BuildInfo.toJson)

Create a Table/Query with JPA/Hibernate in Play Framework

So I have Play Framework running at the moment with JPA and Hibernate. I'm completely new to both and the tutorials I've found around the web are above my head.
How in the world can I send a simple query or create a table? This is example code I've written up and I get: "RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread."
package controllers;
import play.Logger;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import play.db.jpa.JPA;
import play.mvc.Controller;
import play.db.*;
public class Database {
public static void initbuild() {
Logger.info("Checking database structure. The database will be restructured if not in the correct format.");
JPA.em().createQuery("create table test");
}
}
Le'ts start saying that you don't want to build the tables by yourself, just write your models under the models package annotating them by #Entity and the JPA plugin will automagically generate the tables matching the models that you have defined.
As for the error the error is raised cause you should annotate the method with the #Transactional annotation.
As stated in the official doc http://www.playframework.com/documentation/2.3.x/JavaJPA
"Every JPA call must be done in a transaction so, to enable JPA for a particular action, annotate it with #play.db.jpa.Transactional. This will compose your action method with a JPA Action that manages the transaction for you"
Hope it helped btw reading the doc and have a look to the computer-jpa example is suggested

what is the reason for Jersy RESTFUL web service output in XML format

Hi I am new to Jersy restful web service. I simply created one restful web service with pojo class. I did not mention where ever in my code for xml format but I got output as xml format in browsr.
Please HELP ME reason of output showing XML format... I give my code below.
Class Order:
package shopping.cart.om;
public class Order
{
public Map<String, Order> getModel(){
return contentProvider;
}
}
Class OrdersService:
package shopping.cart.service;
import java.util.ArrayList;
import java.util.List;
import shopping.cart.dao.OrderDao;
import shopping.cart.om.Order;
}
}
<id>1</id>
</order>
</orders>
The reason is that XML is the default format of the output, most likely. Check this question: How to set to default to json instead of xml in jersey?
The question is what type of data you expect after hitting restful endpoint , if XML then you have to tell it in method level , f JSON or other you can also define this in your method , example
package com.hello.demo;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
#POST
#Path("/test")
#Consumes(MediaType.TEXT_XML)
#Produces(MediaType.TEXT_XML)
public String consumeTest (String requestMessage) {
return requestMessage;
}
}
Suppose you want simple text as output then
use :
#Produces(MediaType.TEXT_PLAIN)
in method level .
For detail please visit : Jersey
Apart from it if you are interested about different media type supported by jersey you may visit : media types

Inout and Out parameters in WebServices

I'm using Inout and Out parameters in the ServiceEndPointInterface(SEI).
Here is the method signature.
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.WebParam.Mode;
import javax.jws.Oneway;
import javax.xml.rpc.holders.*;
#WebMethod
#Oneway
public #WebResult void TestDomainCls( #WebParam (mode=Mode.INOUT) IntegerWrapperHolder inpuInt );
And I have implemented this method in the EJBBean. And I have exposed this EJBBean as a webservice using annotation.
While deploying this EAR in JBOSS 5.,it's throwing the error like
Caused by: java.lang.IllegalStateException:
Cannot synchronize to any of these methods:
public abstract java.lang.String MURCOMP.MURCOMP_SEI.serv_20_search1(java.lang.String,javax.xml.rpc.holders.StringHolder)
OperationMetaData:
qname={http://MURCOMP/}serv_20_search1
javaName=serv_20_search1
style=rpc/literal
oneWay=false
soapAction=
ParameterMetaData:
xmlName=arg0
partName=arg0
xmlType={http://www.w3.org/2001/XMLSchema}string
javaType=java.lang.String
mode=IN
inHeader=false
index=0
ParameterMetaData:
xmlName=arg1
partName=arg1
xmlType={http://www.w3.org/2001/XMLSchema}anyType
javaType=java.lang.Object
mode=OUT
inHeader=false
index=1
ReturnMetaData:
xmlName=return
partName=return
xmlType={http://www.w3.org/2001/XMLSchema}string
javaType=java.lang.String
mode=OUT
inHeader=false
index=-1
at org.jboss.ws.metadata.umdm.OperationMetaData.eagerInitialize(OperationMetaData.java:491)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitializeOperations(EndpointMetaData.java:559)
at org.jboss.ws.metadata.umdm.EndpointMetaData.initializeInternal(EndpointMetaData.java:543)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitialize(EndpointMetaData.java:533)
at org.jboss.ws.metadata.umdm.ServiceMetaData.eagerInitialize(ServiceMetaData.java:433)
at org.jboss.ws.metadata.umdm.UnifiedMetaData.eagerInitialize(UnifiedMetaData.java:194)
at org.jboss.wsf.stack.jbws.EagerInitializeDeploymentAspect.start(EagerInitializeDeploymentAspect.java:48)
at org.jboss.wsf.framework.deployment.DeploymentAspectManagerImpl.deploy(DeploymentAspectManagerImpl.java:129)
at org.jboss.wsf.container.jboss50.deployer.ArchiveDeployerHook.deploy(ArchiveDeployerHook.java:76)
at org.jboss.wsf.container.jboss50.deployer.AbstractWebServiceDeployer.internalDeploy(AbstractWebServiceDeployer.java:60)
at org.jboss.wsf.container.jboss50.deployer.WebServiceDeployerEJB.internalDeploy(WebServiceDeployerEJB.java:113)
at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
... 30 more
17:40:12,483 ERROR [ProfileServiceBootstrap] Failed to load profile: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):
This error is coming only if I'm using inout or out parameters in my method.
Can anyone suggest me.,where I'm going wrong or Is there anything missing with respect to INOUT and OUT parameters in Web Services
enter code here
I have got the solution.,I have used javax.xml.ws.Holder instead of the javax.xml.rpc.holders.
The code is as follows
#WebMethod
public #WebResult String testINout(#WebParam Holder holder);
But I didnt got the actual solution, why if I use javax.xml.rpc.holders such exception has occured. Anyway the alternate way I found to work on..

Categories

Resources