I am currently evaluating Spring Data REST.
I started with this simple example: link
It works out of the box. But now, in the next step, I wanted to try some validation to see how the framework reacts. So I simply annotated the Personclass:
#Size(min = 2, message = "{test.error.message}")
private String firstName;
The validation itself is working, I get an error message. The message is resolved if I put a file called ValidationMessages.properties in the root of the classpath (see here why).
Now, instead of having the files in the root I wanted to place them in a subfolder (e.g. lang/ValidationMessages.properties) and use Spring MessageSource instead of the default approach.
After some research I found the following question:
MessageInterpolator in Spring
Unfortunately using the following bean definitions does not work:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>lang/ValidationMessages</value>
</list>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
</beans>
The corresponding dependencies inside the pom.xml (just in case):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Does anyone know what I am missing? Could it be due to the fact that I am not using Spring MVC but Spring Data REST? If yes, is there a way of getting this to work?
After some additional investigation (and a lot of searching) I found a solution to the issue.
PROBLEM
Hibernate does not use beans for the validator factory, thats why the LocalValidatorFactoryBean is not used.
For more details look into org.hibernate.cfg.beanvalidation.TypeSafeActivator#activate(ActivationContext activationContext)
FIRST APPROACH
You can actually specify which factory to use by using this property: javax.persistence.validation.factory
Unfortunately this can't be used (yet) inside Spring Boot's application.properties.
(see this Issue on GitHub)
SOLUTION
Using the workaround described in the linked GitHub issue works.
You have to provide a configuration for Hibernate:
#Configuration
public class HibernateConfig extends HibernateJpaAutoConfiguration {
#Autowired
private ValidatorFactory validator;
#Override
protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
super.customizeVendorProperties(vendorProperties);
vendorProperties.put("javax.persistence.validation.factory", validator);
}
}
Using this approach the messages get resolved correctly.
HibernateJpaAutoConfiguration can not work anymore (after sprint boot 2.10)
You can do like this if you are using Spring Boot 2.1.0+:
#Configuration
#Lazy
class SpringValidatorConfiguration {
#Bean
#Lazy
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
return new HibernatePropertiesCustomizer() {
#Override
public void customize(Map<String, Object> hibernateProperties) {
hibernateProperties.put("javax.persistence.validation.factory", validator);
}
};
}
}
The idea from Spring Boot 2.0.0 M6 - Add Hibernate Interceptor
and Spring Boot - Hibernate custom constraint doesn't inject Service
Related
I have a project in grails 3, that has a project spring dependency, in the spring project, xml beans are configured. How should import the bens in grails architecture?
build.gradle
dependencies {
compile (project(':spring-project')) { transitive = false }
}
settings.gradle
includeFlat 'spring-project'
I tried the following ways:
in the resources.groovy load the beans:
beans = {
importBeans('path/to/beans-definition.xml')
}
in the resources.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<import resource="path/to/beans-definition.xml" />
</beans>
Besides that, in the spring project, beans using java annotation are configured. The beans is not working in grails app, even by setting the spring project packages in the conponent scan.
#ComponentScan(basePackages = ["package.spring.project.beans"])
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
But, nothing works. Any help would be appreciated..
Beans can also be configured using a grails-app/conf/spring/resources.xml. In earlier versions of Grails this file was automatically generated for you by the run-app script, but the DSL in resources.groovy is the preferred approach now so it isn’t automatically generated now. But it is still supported - you just need to create it yourself.
<bean id="myBean" class="my.company.MyBeanImpl">
<property name="someProperty" value="42" />
<property name="otherProperty" value="blue" />
further check this link
I would like Spring to rollback a transaction on methods annotated with #Transactional in case the method throws a checked exception. An equivalent of this:
#Transactional(rollbackFor=MyCheckedException.class)
public void method() throws MyCheckedException {
}
But I need this behavior to be default for all #Transactional annotations without the need to write it everywhere. We are using Java to configure Spring (configuration classes).
I tried the configuration suggested by spring documentation, which is only available in XML. So I tried to create this XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="com.example.MyCheckedException" />
</tx:attributes>
</tx:advice>
</beans>
... and import it via #ImportResource. Spring did recognize and parse the file (I had some errors in it at first), but it doesn't work. The behavior of #Transactional has not changed.
I also tried defining my own transaction property source, as suggested in this answer. But it also used the XML configuration so I had to transform it into Java like this:
#Bean
public AnnotationTransactionAttributeSource getTransactionAttributeSource() {
return new RollbackForAllAnnotationTransactionAttributeSource();
}
#Bean
public TransactionInterceptor getTransactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);
return transactionInterceptor;
}
#Bean
public BeanFactoryTransactionAttributeSourceAdvisor getBeanFactoryTransactionAttributeSourceAdvisor(TransactionAttributeSource transactionAttributeSource) {
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource);
return advisor;
}
This also didn't work - Spring kept using its own transaction property source (different instance than the one which was created in the configuration).
What is the correct way to achieve this in Java?
You should rather implement own annotation - reference
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Transactional(rollbackFor=MyCheckedException.class)
public #interface TransactionalWithRollback {
}
I use ActiveMQ as Embedded with Spring Boot.
It seems the Broker is created trough an ActiveMQConnectionFactory.
I understand that the way to configure the broker is to set parameters in the query with broker. as described here : http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html
I would like to setup some features about the DLQ, so it's in the destinationPolicy attribute, but the attribute type is not a simple type but a complex type, how can I write the query parameter to disable DLQ, please ?
Complementing #Petter and #April answers, below the same solutions but with more complete samples:
1. Petter solution, import activemq.xml at connnection factory url
build.gradle
ext {
springBootVersion = "1.5.3.RELEASE"
activeMQVersion = "5.14.5"
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-activemq:${springBootVersion}")
compile("org.apache.activemq:activemq-broker:${activeMQVersion}")
testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
testCompile group: 'org.apache.activemq', name: 'activemq-spring', version: "${activeMQVersion}"
testCompile("junit:junit:4.12")
}
src/main/resources/activemq.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd
">
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="broker1" persistent="false" >
<transportConnectors>
<transportConnector name="vm" uri="vm://broker1"/>
</transportConnectors>
</broker>
</beans>
Config.java
#EnableJms
#SpringBootApplication
#EnableAutoConfiguration
#Configuration
public class Config {}
application.properties
spring.activemq.broker-url=vm://broker1?brokerConfig=xbean:activemq.xml
2. April solution, import activemq.xml at Spring Configuration
Just remove application.properties then add #ImportResource("classpath:activemq.xml") entry to Config.java
Config.java
#EnableJms
#SpringBootApplication
#EnableAutoConfiguration
#Configuration
#ImportResource("classpath:activemq.xml")
public class Config {}
Good question. The properties on the vm-transport for auto-broker creation are great, but only up to a point which I think you have hit.
My suggestion is that you define the broker configuration as you normally would have done in XML and then just refer to this xml in the URI. Destination policies are indeed a complex structure and I don't see how it would be a good idea to define them with simple query params even if it was possible.
vm://localhost?brokerConfig=xbean:activemq.xml
I had this problem and solved it by using a spring configuration file. In my case, I wanted to configure my broker to persist.
I added the needed libs in my pom: including activemq-broker, activemq-spring, spring-jms (and in my case, activemq-leveldb-store).
My spring xml file looked like this:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="xyz">
<persistenceAdapter>
<levelDB directory="activemq-data"/>
</persistenceAdapter>
<transportConnectors>
<transportConnector uri="vm:localhost?persistent=true" />
</transportConnectors>
</broker>
</beans>
And I registered the spring file in one of my configuration classes:
#ImportResource("activemq-spring.xml")
That did the job.
I tried the xbeans solution first, but I got stuck because I was missing some xbeans classes, and I didn't know if it was a version thing or what. I'm using activemq 5.12.1
I am inheriting a project from a developer who left, and I am trying to understand GWT and Spring Framework.
The original problem that lead me to this path: GWT had one module where I loaded ALL third party javascripts... that could result in conflicts. Example, I would include chart drawing libraries, etc. all in one page.
Possible solutions: Have the chart drawing library in an iframe so that it would not conflict with other third party libraries of javascript... OR open the page in a new window.
I decided to go with a new window.
So I did this:
Window.Location.assign(GWT.getHostPageBaseURL()
+ "chartModule.html?gwt.codesvr=127.0.0.1:9997/");
However, in my new chartModule.java (GWT) the problem I have is I do not have the beans/classses defined in (Spring framework) applicationContext.xml anymore:
#Autowired
ApplicationContext applicationContext;
And applicationContext is null after I have changed the host page url... so I do not have any beans that I tried autowiring...
Is it possible to reload the beans from applicationContext.xml??
Here is my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- This file has properties that are used by other XML files loaded via ${var name} syntax -->
<context:property-placeholder location="/WEB-INF/classes/environment.properties" />
<import resource="spring-security-cas.xml" />
<!-- Scans the classpath for annotated components that will be auto-registered
as Spring beans. For example #Controller and #Service. Make sure to set the
correct base-package -->
<context:component-scan base-package="com.javamango.sixtydegrees" />
<import resource="mongo-config.xml" />
<import resource="rabbitmq-context.xml" />
<import resource="spring-mail.xml" />
</beans>
You cannot use spring beans on client side. If you want retriewe some data from spring in gwt, you can do this at two ways:
1) use server side library like gwt-sl to inject spring beans in gwt servlet
#Service("greetingService")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService
{
#SuppressWarnings("unused")
private static final Logger LOGGER = LoggerFactory.getLogger(GreetingServiceImpl.class);
#Autowired
UserFileService userFileService;
#Autowired
UserService userService;
}
now you can autowire spring beans and obtain data via gwt-rpc
2) put data via jsp in hidden html form fields and retriewe data from it
<input type="hidden" value="7" id="documentid"/>
String id = (InputElement) (Element) DOM.getElementById("documentid").value
I use JIBX to create my entity classes from XSD files. It is configured in pom.xml and creates classes when I do a "maven: compile"
I also use spring-ws. When I test my web service with SOAPUI I get the infamous error;
"No adapter for endpoint GetTransactionsResponse getTransactions(GetTransactionsRequest), Is your endpoint annotated with #Endpoint, or does.."
I checked all the threads here about that error but didn't help.
I have one Parent.xsd and it imports 2 child xsd's. They are all in the same folder. This is how my spring-ws-servlet looks like;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean name="xsdCollection" class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="xsds">
<list>
<value>/WEB-INF/Parent.xsd</value>
</list>
</property>
</bean>
<context:component-scan base-package="mypackage"/>
<sws:annotation-driven/>
<sws:dynamic-wsdl id="my" portTypeName="myResource" locationUri="/ws/my"
targetNamespace="myschame">
<sws:xsd location="/WEB-INF/Parent.xsd"/>
</sws:dynamic-wsdl>
<sws:interceptors>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor">
<property name="logRequest" value="true"/>
<property name="logResponse" value="true"/>
</bean>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="xsdSchemaCollection" ref="xsdCollection"/>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="true"/>
</bean>
</sws:interceptors>
This is my endpoint class;
#Endpoint
public class TransactionsEndpoint {
public static final String NAMESPACE = "nmapespace";
#PayloadRoot(namespace = NAMESPACE, localPart = "getTransactionsRequest")
#ResponsePayload
public GetTransactionsResponse getTransactions(#RequestPayload GetTransactionsRequest request) {
GetTransactionsResponse transactionsResponse = new GetTransactionsResponse();
return transactionsResponse;
}
}
GetTransactionsResponse/Request classes created by JIBX.
My wsdl looks like this;
<wsdl:operation name="getTransactions"><wsdl:input message="tns:getTransactionsRequest" name="getTransactionsRequest">
</wsdl:input><wsdl:output message="tns:getTransactionsResponse" name="getTransactionsResponse">
</wsdl:output></wsdl:operation>
pom file is;
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.0.2</version>
</dependency>
I am not sure if the problem is because there are 3 xsd files and something goes wrong between those or it is a configuration problem with JIBX because When I try to use JAXB instead of JIBX, it worked!
Endpoint mappings are stored in a hashmap with a key based on the namespace and the local part of the #PayloadRoot annotation (see code below). You currently have (what I assume is) a typo in the namespace of the java class... nmapespace instead of namespace.
If this does not match up with what is located in your xsd and subsequently published wsdl (which are not shown), then the mapping would not be found. This is one (of the many) possible reasons that you would get that error.
public class PayloadRootAnnotationMethodEndpointMapping extends
AbstractAnnotationMethodEndpointMapping<QName> {
...
#Override
protected QName getLookupKeyForMethod(Method method) {
PayloadRoot annotation = AnnotationUtils.findAnnotation(method, PayloadRoot.class);
if (annotation != null) {
QName qname;
if (StringUtils.hasLength(annotation.localPart()) && StringUtils.hasLength(annotation.namespace())) {
qname = new QName(annotation.namespace(), annotation.localPart());
}
else {
qname = new QName(annotation.localPart());
}
return qname;
}
else {
return null;
}
}
If this is not the problem, you may need to add some more info to the question (soap request, xsds, wsdl).
I also had similar issue(spent several days) however in my case issue being Spring WS and Spring versions being incompatible, check whether your Spring WS and Spring versions match.