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.
Related
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
I have written some code in which i am consuming another webService and sending a request to that webservice using WebServiceTemplate.
But i am getting the following exception when that code trgiggers.
I have checked the lib's of Spring Core & everything seems ok but dont know why this service is throwing such exception.
ApplicationContxt:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12" />
</property>
</bean>
<bean id="manageContactService" class="com.canaldigital.tsi.managecontacts.serviceprovider.ManageContactService">
<property name="manageContactsWSTemplate" ref="manageContactsWSTemplate" />
</bean>
<bean name="manageContactsWSTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory" />
<property name="defaultUri" value="http://tsi-vip-abc.com:7111/abc_v2/ProxyService?WSDL" />
<property name="marshaller" ref="manageContactMarshaller" />
<property name="unmarshaller" ref="manageContactUnmarshaller" />
</bean>
Service:
public class ManageContactService extends WebServiceGatewaySupport {
private WebServiceTemplate manageContactsWSTemplate;
public WebServiceTemplate getManageContactsWSTemplate() {
return manageContactsWSTemplate;
}
public void setManageContactsWSTemplate(WebServiceTemplate manageContactsWSTemplate) {
this.manageContactsWSTemplate = manageContactsWSTemplate;
}
public void sendNPSReminder(String phoneNum, String customerNum, String countryCode ) {
SendNPSReminderRequestType sendNPSReminderRequest = new SendNPSReminderRequestType();
Contact contact = new Contact();
sendNPSReminderRequest.setCountryCode(CountryCodeCV.NO);
contact.setPhone(new BigInteger(phoneNum));
sendNPSReminderRequest.setContact(contact);
sendNPSReminderRequest.setCustomerNumber(customerNum);
try{
JAXBElement<SendNPSReminderResponseType> response = (JAXBElement<SendNPSReminderResponseType>) manageContactsWSTemplate.marshalSendAndReceive(sendNPSReminderRequest);
}catch (Exception e) {
e.printStackTrace();
}
}
}
StackTrace:
java.lang.IllegalStateException: Could not load 'class path resource [org/springframework/ws/client/core/WebServiceTemplate.properties]': class path resource [org/springframework/ws/client/
ore/WebServiceTemplate.properties] cannot be opened because it does not exist
at org.springframework.ws.support.DefaultStrategiesHelper.<init>(DefaultStrategiesHelper.java:78)
at org.springframework.ws.support.DefaultStrategiesHelper.<init>(DefaultStrategiesHelper.java:88)
at org.springframework.ws.client.core.WebServiceTemplate.initDefaultStrategies(WebServiceTemplate.java:338)
at org.springframework.ws.client.core.WebServiceTemplate.<init>(WebServiceTemplate.java:130)
at org.springframework.ws.client.core.support.WebServiceGatewaySupport.<init>(WebServiceGatewaySupport.java:65)
at com.canaldigital.tsi.managecontacts.serviceprovider.ManageContactService.<init>(ManageContactService.java:24)
at com.canaldigital.tsi.managecontacts.utils.CDCommonTasksJob.launch(CDCommonTasksJob.java:97)
at sun.reflect.GeneratedMethodAccessor2044.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:82)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:440)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
That resource is part of spring-ws-core dependency, so it must work out of the box, but, I guess your are using WLS 11 (as you tagged it). So, it is possible that you are not using the spring dependency you have but instead spring packaged into WebLogic.
try to avoid this using WEB-INF/weblogic.xml descriptor as following:
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
<container-descriptor>
<prefer-application-packages>
<package-name>org.springframework.*</package-name>
</prefer-application-packages>
<prefer-application-resources>
<resource-name>org.springframework.*</resource-name>
</prefer-application-resources>
</container-descriptor>
</weblogic-web-app>
With this configuration you are telling WLS to use your packages and resources for org.springframework.*.
Hope it helps!
I want to send JSON from my controller. I have the following configuration.
spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<context:component-scan base-package="com.castle.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
.js :
function testAjax() {
var data = {userName: "MyUsername", password:"Password"};
$.ajax({
url: 'ajax/test.htm',
dataType : 'json',
type : 'POST',
contentType: "application/json",
data: JSON.stringify(data),
success: function(response){
alert('Load was performed.');
}
});
}
UserTest.java:
public class UserTest {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
TestAjaxController.java :
#Controller
#RequestMapping("/ajax")
public class TestAjaxController {
#RequestMapping(method = RequestMethod.POST, value = "/test.htm")
public #ResponseBody
UserTest testAjaxRequest(#RequestBody UserTest user) {
return user;
}
}
pom.xml :
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
When i do this request, i get in my Controller JSON represented as UserTest object. But on return :
HTTP Status 406 - The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
What i'm doing wrong? I know, there is a lot of questions about such cases, but i can't fix it for 2 days...
UPDATE
I Have found the solution!!
It's only need to return an Object. Not a User object or something.
But
return Object;
public #ResponseBody Object testAjaxRequest(#RequestBody UserTest user) {
List<UserTest> list = new ArrayList<>();
list.add(user);
list.add(user);
list.add(user);
return list;
The main issue here is that the path "/test.htm" is going to use content negotiation first before checking the value of an Accept header. With an extension like *.htm, Spring will use a org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy and resolve that the acceptable media type to return is text/html which does not match what MappingJacksonHttpMessageConverter produces, ie. application/json and therefore a 406 is returned.
The simple solution is to change the path to something like /test, in which content negotiation based on the path won't resolve any content type for the response. Instead, a different ContentNegotiationStrategy based on headers will resolve the value of the Accept header.
The complicated solution is to change the order of the ContentNegotiationStrategy objects registered with the RequestResponseBodyMethodProcessor which handles your #ResponseBody.
I had the same problem in the end it was the version of org.codehaus.jackson 1.9.x,
when I switched from 1.9.x jackson to 2.x (fasterxml) in my pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
also is necesary : <mvc:annotation-driven />
I ran into this issue when upgrading Spring in a legacy project. The .html suffix of the AJAX endpoints (which were trying to return JSON) were indeed forcibly made to try to produce HTML due to the suffix, and since the handlers were returning an object, the request ended in a 406 error since Spring couldn't figure out how to make HTML out of a plain Java object.
Instead of altering the endpoint suffixes, or doing a complete configuration of a custom ContentNegotiationStrategy, making this change was enough:
<mvc:annotation-driven />
changed to:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
Adding these lines to context configuration solved the same issue for me:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
I used Spring 4.1.x and Jackson 2.4.2
Make sure to use produces = "application/json" in your annotations.
im using spring for my java web app. the site has got bigger and i would like to set some configurations.
i have been researching and came across things like document builder factory, replacing spring xml with java config and others. i dunno where to start.
im thinking of implementing the configurations in xml (WEB/newConfig.xml) and have it read by the java beans. basically i wanna input my cofiguration values into xml and have it load by a java bean so that i can use it in controllers and jstl.
im just giving some examples here. for example xml configurations:
<property name="numberOfCars" value="3" />
<property name="webSiteName" value="New Spring Web App" />
....
and i read it in my java class:
class Config {
public getNumberOfCars() {
return numOfCars;
}
public getWebSiteName() {
return webSiteName;
}
}
where should i start and what online materials can i read?
==============================
update
here is what i have created.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="/WEB-INF/your_prop_file.properties" />
<bean id="ConfigMgr" class="org.domain.class.ConfigMgr">
<property name="username" value="${username}">
</bean>
</beans>
you_prop_file.properties
username=hello world name
ConfigMgr.java
public class ConfigMgr {
private String username;
...getter
...setter
}
in my controller, here is what i did:
ConfigMgr config = new ConfigMgr();
sysout.out.println(config.getUsername());
i am getting null and i am sure im missing something here. where should i set the username value to the ConfigMgr class?
Spring Java configuration is a newer feature that allows you to configure your Spring application using Java classes instead of XML files. Its just an alternative for XML configuration. XML way is equally feature rich.
From what I could figure out from your problem, you want to move the hardcoded values of params (numberOfCars,webSiteName.. ) outisde your configuration file.
If that is the case, you don't have to go that far.
Just use :-
<context:property-placeholder location="classpath:your_prop_file.properties" />
in your spring xml file and replace the param values like:-
<property name="webSiteName" value="${website.name}" />
You need to have a your_prop_file.properties file in your classpath with enteries like:-
website.name=New Spring Web App
You are not injecting the ConfigMgr bean that you created in XML file.
What you are doing is you are creating a new Object in controller which does not have a clue about properties file.
Now you can inject it using either #Autowired inside your controller or through xml configuration.
There are plenty of examples available in google on basic spring dependency injection.
I have an application in which I use spring 3.0.2 and ibatis. Now, I need to integrate ehcache with my code. I tried this link but couldnt get it working. I would prefer someone to give me the details of the jars required, xml configurations to be done and code changes if required.
Upgrade to the latest spring 3.1 milestone - it has built-in cache support through annotations - see here
Apart from that, you can always use the EhCacheFactoryBean
To implement this in your application, follow these steps:
Step 1:
Add the jars to your application as listed on the Ehcache Annotations for Spring project site.
Step 2:
Add the Annotation to methods you would like to cache. Lets assume you are using the Dog getDog(String name) method from above:
#Cacheable(name="getDog")
Dog getDog(String name)
{
....
}
Step 3:
Configure Spring. You must add the following to your Spring configuration file in the beans declaration section:
<ehcache:annotation-driven cache-manager="ehCacheManager" />
Please refer to Ehcache site for complete details.
To integrate Ehcache just follow below steps
1 - Add Dependency in pom XML file
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.9</version>
</dependency>
2 - create an xml file called spring-cache.xml put it in the resources folder
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
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://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
</beans>
3 - as you can see we are using reference of ehcache.xml so create file and put it in resources folder
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<cache name="users" maxEntriesLocalHeap="5000"
maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20"
timeToIdleSeconds="200" timeToLiveSeconds="500"
memoryStoreEvictionPolicy="LFU" transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
so can see create a cache for "users" so that can use wherever user list is queried from the database
4 - use it like below code
#Cacheable(value="users")
public List<User> userList() {
return userDao.findAll();
}
so that's it the same way you can implement cache wherever is required
still have some doubt or confusion see live demo
Integrate EhCache in Spring MVC