Jackson Jaxb Json with Apache CXf - json

I am using Jackson Jaxb JSON in my REST project with Apache CXF
JacksonJaxb version . 1.7.0
Apache CXF 2.3.1
I am using following code to return from my method.
#GET
#Consumes({ "application/json", "application/xml", "text/xml" })
#Path("/job/autosuggest")
#Override
public Response getSuggestions(String searchField, Integer resPerPage, String typeCont)
{
List<String> respo = new ArrayList<String>();
respo.add("Atish");
respo.add("Narlawar");
respo.add("India");
return Response.ok(respo).build();
}
Now issue is coming when I compile and run the code on jetty, I get stuck with
DEBUG o.s.b.f.s.DefaultListableBeanFactory [] Finished creating instance of bean 'org.apache.cxf.phase.PhaseManager'
org.codehaus.jackson.map.JsonMappingException: Incompatible types: declared root type ([simple type, class javax.ws.rs.core.Response]) vs java.util.ArrayList
This is not particular to array or wrapper, but any object If I pass rather than String in Response.ok(object) fails to parse.
My configuration is
<util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
<entry key="http://services.institute.com" value=""/>
<entry key="http://cxf.apache.org/bindings/xformat" value="cxf"/>
</util:map>
<bean id="jsonInputFactory" class="org.codehaus.jettison.mapped.MappedXMLInputFactory">
<constructor-arg ref="jsonNamespaceMap"/>
</bean>
<bean id="jsonOutputFactory" class="org.codehaus.jettison.mapped.MappedXMLOutputFactory">
<constructor-arg ref="jsonNamespaceMap"/>
</bean>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
<jaxrs:server id="jobsearch" address="/">
<jaxrs:serviceBeans>
<ref bean="jobSearchService" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="text" value="text/xml"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
</jaxrs:extensionMappings>
<jaxrs:languageMappings/>
<jaxrs:properties>
<entry key="javax.xml.stream.XMLInputFactory">
<ref bean="jsonInputFactory"/>
</entry>
<entry key="javax.xml.stream.XMLOutputFactory">
<ref bean="jsonOutputFactory"/>
</entry>
</jaxrs:properties>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
I am not sure how to proceed on this issue. I already lost 1/2 day to get some workaround. Any help will be appreciated.
Thanks in advance !

At last I got the answer.
The issue is with the version itself.
JacksonJaxb has reported bug in 1.7.0.
I updated the version to higher...in my case it is 1.8.5 and it got fixed.
Hope this helps.
Thanks
Atish

Related

WSO2 Couldn't save JSON payload

I use WSO2 EI 6.6.0 and Micro Integrator 4.0.0. I have an api to get data, but for this I need to authenticate and get a token to use in a further request. for authentication I use separately created api and call it from first flow. For authentication I need to create a payload and send it in the request body to the server, but I get an error. The problem is repeated on different servers and I do not understand what is the reason.
My Auth API looks like this:
<api context="/auth" name="AuthGateway" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/gateway">
<inSequence>
<payloadFactory media-type="json">
<format>{"username": "adm", "password": "admin"}</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="post" uri-template="https://1537-212-90-188-166.ngrok.io/auth/login">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<log separator="
">
<property expression="json-eval($)" name="log_auth"/>
</log>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
My Data API looks like this:
<resource methods="GET" uri-template="/fieldCard?*">
<inSequence>
<propertyGroup>
<property name="uri.var.version" expression="$url:version" scope="default" type="STRING"/>
<property name="uri.var.size" expression="$url:size" scope="default" type="STRING"/>
<property name="uri.var.page" expression="$url:page" scope="default" type="STRING"/>
</propertyGroup>
<log level="full" separator="
">
<property name="version" expression="$ctx:uri.var.version"/>
<property name="size" expression="$ctx:uri.var.size"/>
<property name="page" expression="$ctx:uri.var.page"/>
</log>
<call>
<endpoint>
<http method="GET" uri-template="http://localhost:8290/auth/gateway"/>
</endpoint>
</call> .......
In the console I get messages:
[2022-05-02 19:16:12,962] ERROR {JsonStreamBuilder} - Error occurred while processing document for application/json java.lang.reflect.InvocationTargetException
Caused by: org.apache.axis2.AxisFault: #Can not parse stream. MessageID: urn:uuid:7ac6a4c9-0ed1-4973-87d4-69dad6d4a950. Error>>> #getNewJsonPayload. Could not save JSON payload. Invalid input stream found. Payload is not a JSON string.
org.apache.synapse.commons.json.JsonStreamBuilder.processDocument(JsonStreamBuilder.java:43)
... 21 more
Caused by: org.apache.axis2.AxisFault: #getNewJsonPayload. Could not save JSON payload. Invalid input stream found. Payload is not a JSON string.
[2022-05-02 19:16:12,963] ERROR {DeferredMessageBuilder} - Error building message org.apache.axis2.AxisFault
[2022-05-02 19:16:12,964] ERROR {RelayUtils} - Error while building Passthrough stream org.apache.axis2.AxisFault
[2022-05-02 19:16:12,964] ERROR {SequenceMediator} - {api:AuthGateway} Error while building message. Error while building Passthrough stream org.apache.axis2.AxisFault: Error while building Passthrough stream
I removed the detailed stack trace. If it is needed to understand the problem - I can add it.
P.S. When calling the auth API directly I get the token and the response is correct.
[2022-05-02 19:20:17,560] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: http://www.w3.org/2005/08/addressing/anonymous
WSAction:
SOAPAction:
MessageID: urn:uuid:817185a7-39a4-4418-a696-72473672997d
Direction: request
log_auth = {"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMyIsInVzZXJfbmFtZSI6Im...
Payload: {"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMyIsInVzZXJfbmFtZSI6Im...
Please help me understand the problem.
UPD: I reinstalled the server because I thought the error might be there or I broke the config files. created a new project and wrote a new same thread, but the error still repeats.
Good. The problem was that Postman has a Header Accept-Encoding option. When this parameter was disabled, the request became correct and the response began to come without errors!

How to configure MySQL JTA resource in Spring, JPA and Hibernate for Glassfish v3?

My application is able to perform the read operations but unable to write to the database. How should I configure the JTA transaction manager? I'm using the following: Glassfish 3.1.2.2, Hibernate 4.2.21, Spring 4.2.5, MySQL 5.6 and JDK 1.6. I've tried the following configuration:
persistence.xml
<persistence-unit name="appPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/app</jta-data-source>
<properties>
<!--<property name="hibernate.transaction.jta.platform"-->
<!--value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>-->
<!--<property name="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"/>-->
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
</properties>
</persistence-unit>
web.xml
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/appPersistenceUnit</persistence-unit-ref-name>
<persistence-unit-name>appPersistenceUnit</persistence-unit-name>
</persistence-unit-ref>
applicationContext.xml
<jee:jndi-lookup id="entityManagerFactory"
jndi-name="persistence/appPersistenceUnit"/>
<tx:jta-transaction-manager/>
<tx:annotation-driven/>
MyDAOImpl.java
#PersistenceContext(type = PersistenceContextType.EXTENDED, name = "appPersistenceUnit")
protected EntityManager entityManager;
#Override
public int save(final MyEntity myEntity) {
try {
entityManager.merge(myEntity);
}
catch (Exception e) {
LOGGER.error("Error while saving MyEntity", e);
}
return 1; // id of the created element.
}
On trying to save the entity, I get the following error:
Cannot join transaction: do not override hibernate.transaction.factory_class
Please guide me. What am I doing wrong?

Compression for Spring remoting with JSON

I am trying to enable compression for json responses from server. Currently i see the response but not compressed, below i have mentioned the config.
Server: Jetty
Servlet.xml
<bean name="/TaskSearchApp.json" class="com.googlecode.jsonrpc4j.spring.JsonServiceExporter">
<property name="service" ref="taskSearchAppBean"/>
<property name="serviceInterface" value="com.webmethods.caf.TaskSearchAppInterface"/>
</bean>
Web.xml
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>*json</url-pattern>
</filter-mapping>
Client-bean.xml
</bean>
<bean id="taskSearchAppBeanJSON" class="com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8888/TaskSearchApp/TaskSearchApp.json"></property>
<property name="serviceInterface" value="com.webmethods.caf.TaskSearchAppInterface"></property>
</bean>
Ok I could sort this out by adding
extraHttpHeaders.put("Accept-Encoding","gzip, deflate");
But now i get the error while parsing the gzipped response
Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: com.googlecode.jsonrpc4j.NoCloseInputStream#700a4488; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1369)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:532)
at com.fasterxml.jackson.core.base.ParserMinimalBase._throwInvalidSpace(ParserMinimalBase.java:478)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2508)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:617)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2926)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2873)

HttpMediaTypeNotAcceptableException with Spring 3.1.2 and Jackson 1.9.11

~R
I'm trying to do Spring MVC REST JSON channel with automatic conversion to JSON using Jackson. Although I'm doing everything as recommended in other StackOverflow questions, I'm getting HttpMediaTypeNotAcceptableException and my beans/lists are not converted to JSON. I'm running the code on Jetty.
I've followed the instruction from UTF-8 encoding problem in Spring MVC, Spring 3.0 making JSON response using jackson message converter and Jackson annotations being ignored in Spring, but none of them enables me to send neither bean nor a List<String>. Both return HTTP status 406. Only the method returning String succeeds with HTTP 200.
My method is:
#RequestMapping(value = "/list", produces = "text/plain; charset=utf-8")
public #ResponseBody List<String> getList() {
return createList();
}
Spring file:
<context:component-scan base-package="de.myapp.rest" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
Adding this section also doesn't help:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
The request HTTP header is generated by jQuery and looks like that:
Accept application/json, text/javascript, /; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101
Firefox/18.0
However, I get HTTP 406 with HTML error message, and the following lines in log file:
Resolving exception from handler [public de.myapp.rest.TestBean
de.myapp.rest.Test.getList()]:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
find acceptable representation
So the question is, what I'm missing here? Why the jackson converter is not registered/how can I debug it? I'm running the code via jetty:run. But it should not matter, since Spring is application-server-independent.
In your #RequestMapping you have produces = "text/plain", but your request header says Accept application/json, text/javascript. There is no match.
Use either produces="application/json" or don't use produces at all. It's main use case is to provide more than one method for the same URL, each producing output in a different format (JSP, JSON, XML ...).

Fluent NHibernate Mapping Exception On Compiling Mapping Document

I have an NHibernate configuration file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="MyProject">
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string_name">MyProject</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>
When creating the Configuration object:
Configuration cfg = new Configuration();
cfg.AddFile(nhibernateConfigurationFile);
I'm getting the following errors when trying to run a persistence test:
System.InvalidOperationException: Could not find the dialect in the configuration
NHibernate.MappingException: Could not compile the mapping document: C:\PathToProject\bin\Debug\NHibernateMyProject.config
Anyone know why it is having trouble reading the nhibernate config file? From what I am able to see, the dialect is definitely in the config....
You should use this dialect: NHibernate.Dialect.MsSql2000Dialect
Try to define the assembly which contains the mapping files:
<mapping assembly="yourAssembly"/>
For the configuration file:
new Configuration().Configure( yourfile.cfg.xml );