Apache - CXF jaxrs-server - unable to hit the resource which is defined first in jaxrs-server endpoint - apache

I'm using Apache-CXF for JAX-RS implementation. I have two resources which are defined in two bean. My jaxrs-server in context.xml os as follow
<jaxrs:server id="serverId" address="/">
<jaxrs:serviceBeans>
<bean id="bean1" class="com.Bean1" />
<bean id="bean2" class="com.Bean2" />
</jaxrs:serviceBeans>
</jaxrs:server>
Interface for Bean1 is as follows -
#Path("/")
public interface IBean1 {
#GET
#Path("/beaninfo1")
#Produces({ MediaType.APPLICATION_XML })
public Response checkBean1();
}
Interface for Bean2 is as follows -
#Path("/")
public interface IBean2 {
#GET
#Path("/beaninfo2")
#Produces({ MediaType.APPLICATION_XML })
public Response checkBean1();
}
I'm unable to hit the resource which is defined in last in serviceBans definition. In this case i'm able to hit Bean2 but not Bean1, getting 404 error, where as if i put Bean2 first and then Bean1, i'm able to hit Bean1 only.
Is there anything wrong with my configuration ?

It is possible to have the same #Path annotation at class level. You need to use a resourcecomparator. Please check this question

Yes. Give them different #Path annotations at the class level.

Related

saslConfig using spring rabbitmq not working

My project needed SSL authentication mechanism to be EXTERNAL(using SSL certificates only and avoiding username/password on rabbitmq). For the connectionfactory bean we gave property name="saslConfig" value= "DefaultSaslConfig.EXTERNAL", but we are getting an error: "Cannot convert value of type [java.lang.String] to required type [com.rabbitmq.client.SaslConfig] for property 'saslConfig': no matching editors or conversion strategy found". we tried other values like value= "com.rabbitmq.client.DefaultSaslConfig.EXTERNAL" and value="EXTERNAL", but still the error persists. Can you please check on the configuration and logs below and provide me your suggestions.
Bean configuration
<rabbit:connection-factory id="connectionFactory" connection-factory="clientConnectionFactory" host="x.y.z.m" port="5671"/>
<bean id="clientConnectionFactory" class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
<property name="useSSL" value="true" />
<property name="saslConfig" value=com.rabbitmq.client.DefaultSaslConfig.EXTERNAL"/>
<property name="sslPropertiesLocation" value="classpath:/rabbitSSL.properties"/></bean>
Logs
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.rabbitmq.client.SaslConfig] for property 'saslConfig': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
The following worked for me (source: https://github.com/spring-projects/spring-boot/issues/6719#issuecomment-259268574):
#PostConstruct
public void init() {
if (rabbitProperties.getSsl().isEnabled() && rabbitProperties.getSsl().getKeyStore() != null) {
cachingConnectionFactory.getRabbitConnectionFactory().setSaslConfig(DefaultSaslConfig.EXTERNAL);
}
}
EXTERNAL is a static variable, not an enum.
Use
"#{T(com.rabbitmq.client.DefaultSaslConfig).EXTERNAL}"
which is a SpEL expression using the type operator (T) to get a reference to the static.
See SpEL
We can use ConnectionFactoryCustomizer in package org.springframework.boot.autoconfigure.amqp
to set sasl config on rabbitmq connection factory on bean creation.
#Autowired
RabbitProperties rabbitProperties
#Bean
ConnectionFactoryCustomizer connectionFactoryCustomizer() {
if (rabbitProperties.getSsl().getEnabled() && rabbitProperties.getSsl().getKeyStore() != null) {
return (connectionFactory) -> connectionFactory.setSaslConfig(DefaultSaslConfig.EXTERNAL)
}
return (connectionFactory) -> connectionFactory.setSaslConfig(DefaultSaslConfig.PLAIN)
}

Set datasource for custom itemWriter (spring batch)

I want to read from a database and write to another database using a custom itemWriter.
How can I set datasource for custom itemWriter?
Please have a look at this sample project Spring Batch Example
Here is sample code for your custom ItemWriter.
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.stereotype.Component;
import com.x.y.z.MyBean;
#Component("myJdbcWriter")
public class MyCustomJdbcWriter extends JdbcBatchItemWriter<MyBean> {
MyCustomJdbcWriter() {
}
#Override
#Resource
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
// define other methods also that you want to override in the same manner
}
Either use auto-detect feature of Spring or you can define a bean in your spring xml configuration file as shown below and use this bean in you respective job.
<bean id="myJdbcWriter" class="com.x.y.z.MyCustomJdbcWriter"
scope="step">
<property name="dataSource" ref="dataSource" />
</bean>

SessionFactory XML configuration works, but not Java configuration. Why?

In a Spring XML configuration, I have the followings:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
....
</bean>
and in a Java class, I have
#Autowired
private SessionFactory sessionFactory;
without a setter. That works.
Now, I change the sessionFactory to a Java configuration as the followings.
#Configuration
#EnableTransactionManagement
#PropertySource({ "classpath:jdbc.properties" })
public class PersistenceConfig {
#Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder lsfb = new LocalSessionFactoryBuilder(dataSource());
lsfb.addAnnotatedClasses(...);
lsfb.setProperties(hibernateProperties());
return lsfb.buildSessionFactory();
}
// ...
}
And I get an error "could not autowire field". Adding a setter doesn't help. Why the sessionFactory can't get autowired with a Java configuration?
BTW, I can work around this problem by having a Java configuration for the DAO as well.
I see that there is no #ComponentScan annotation on your #Configuration class, so probably the problem is in how you import this configuration. Please ensure that all particular beans live in the same context or at least that PersistenceConfig is parent to the context in which you are autowiring SessionFactory
I have added the #ComponentScan annotation and it doesn't solve the problem. The annotation tells Spring to look for any #Components to configure as beans. This problem seems to me is that during the process of creating a bean with #Component, it can't find a bean configured in my Java configuration file which is started in WebAppInitializer.

An EJB is in a JAR but is not found by the WAR next to in in an EAR

I have the structure described below, but I cannot make it so MyWebService has its member myService not null. The code of MyWebService is properly executed when I call the webservice. When I look at the JBoss logs, I keep seeing that MyServiceBean has several JNDI bindings allocated to it.
So how do I bind MyServiceBean to MyWebService?
Thanks!
my-ejb.jar:
#Local
public interface MyServiceBeanLocal {
...
}
#Stateless
public class MyServiceBean implements MyServiceBeanLocal {
...
}
my-web.war:
#Webservice(...)
public class MyWebService {
#EJB
MyServiceBeanLocal myService;
...
}
my-ear.ear:
* my-ear.ear
|-* my-web.war
|-* my-ejb.jar
Have you tried using MyServiceBeanLocal as a Remote interface ? You are trying to use dependency injection from a Web module and for a Local Interface .This is not actually suggested. Anyway, At first try to make the interface #Remote . If still it doesn't work try to use`Remote Look up from the Web module for your Remote interface link
I use CXF. CXF is not an EJB container, hence the issues I got.
I had to manually bind the EJBs, using their full name.

How to set a spring.NET AOP advice before method call

I want to intercept a method call before execution using spring.NET. Let's assume the class/method to be intercepted is:
public class Listener
{
public void Handle()
{
// method body
}
}
This is what I've done (assuming all code is in a namespace called Example):
1.Created the advice:
public class MyAopAdvice : IMethodBeforeAdvice
{
public void Before(MethodInfo method, object[] args, object target)
{
// Advice action
}
}
2.Updated my spring xml configs:
<object id="myAopAdvice" type="Example.MyAopAdvice" />
<object id="listener" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="Target">
<object type="Example.Listener" autowire="autodetect"/>
</property>
<property name="InterceptorNames">
<list>
<value>myAopAdvice</value>
</list>
</property>
</object>
For some reason my Advice code is not getting hit if I put a breakpoint in it. However, if I add some console logging statements within my advice, it seems they are logged, but not at the appropriate time (i.e., before calling Listener.Handle()).
I'm willing to bet my configs are wrong (for once, I may be missing a way to tell the configs to listen for just the Handle method call and not any other method that Listener may have). Any ideas what's wrong?
Declare your Handle method as virtual:
public virtual void Handle() // ...
Your class does not implement any interfaces, which spring.net's default aop mechanism uses to create proxies. When spring.net does not find any interfaces to proxy, it looks for virtual methods to create a proxy for a class.