I have the following class
public class HeaderClass{
#Resource
private WebServiceContext webServiceContext;
public String getUserAgent() {
MessageContext msgCtx = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest)msgCtx.get(AbstractHTTPDestination.HTTP_REQUEST);
return request.getHeader("user-agent")
}
In my service bean class I want to inject this HeaderClass, so that I can use it there as follows:
package mypack;
#Path("/MyService")
public class MyServiceClass {
//May be some annotation has to be given here which I don't know
HeaderClass header;
public void useHeader() {
//Code to use the header
System.out.println(header.getUserAgent());
}
}
I have the following inside beans.xml file
<jaxrs:server id="SampleService" address="/">
<jaxrs:features>
<cxf:logging />
</jaxrs:features>
<jaxrs:serviceBeans>
<ref bean="MyServiceClass"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="MyServiceClass" class="mypack.MyServiceClass"/>
I don't know how to add the property HeaderClass in the bean "MyServiceClass"
I am using apache cxf with spring configuration file (beans.xml).
Please help.
One way to achieve this is to add those lines to your beans.xml:
<bean id="HeaderClass" class="mypack.HeaderClass"/>
<bean id="MyServiceClass" class="mypack.MyServiceClass">
<property name="header" ref="HeaderClass" />
</bean>
You may also need to add a setHeader() method to your MyServiceClass.
Related
Hi we are using Activiti 6.0.0 along with Spring-Boot application on Spring Microservices. I have to write an Unit tests on bpmn files which have DelegateExpression. Can anyone point me the some ideas for UnitTest?
I've done a fully tested activiti project before. upon your question i uploaded it on my github activiti-test, so you can access complete and executable source of this answer. This project is based on Maven, Activiti, Spring, H2 and JUnit. but if you want to run it over Spring Boot so you can see my other sample project Spring-boot kickstart sample.
The activit-test project structure :
Maven Project POM.xml
You can import (activiti-test) project as existing Maven Project in your IDE or anywhere you are comfortable.
Important dependency is :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
.
.
.
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
.
.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
.
.
.
2. applicationContext.xml
The important code block is starting activiti engine (bean: processEngineConfiguration)
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!-- H2 -->
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="driverClass" value="org.h2.Driver" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true" /> <!-- important and valid values : true,false,create-drop -->
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="jobExecutorActivate" value="true" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="true" />
</bean>
3. Create activiti database Schema
At the sample code there is a class bean named InitDb that call
org.activiti.engine.impl.db.DbSchemaCreate.main(null);
4. Prepare Test environment
There is a class named BaseTest to make other testing easy :
first part get instance of activiti service and the second part is a method to deploy a process by BaseTest child class that comes in the next code block.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(BaseTest.APPCONTEXT)
public abstract class BaseTest implements ApplicationContextAware {
public static final String APPCONTEXT = "/applicationContext.xml";
Logger logger = Logger.getLogger(BaseTest.class);
public static ApplicationContext CONTEXT;
#Autowired(required=true)
protected ProcessDeployer processDeployer;
#Autowired
protected MyProcessEngine processEngine;
protected RuntimeService runtimeService;
protected TaskService taskService;
protected RepositoryService repositoryService;
protected HistoryService historyService;
#PostConstruct
public void init() {
runtimeService = processEngine.getProcessEngine().getRuntimeService();
taskService = processEngine.getProcessEngine().getTaskService();
repositoryService = processEngine.getProcessEngine().getRepositoryService();
historyService = processEngine.getProcessEngine().getHistoryService();
}
.
.
.
.
private List<String> deploymentIds = new ArrayList<String>();
#Before
public void deploy()
{
if(deploymentIds.size() > 0)
for(String deploymentId : deploymentIds)
repositoryService.deleteDeployment(deploymentId);
deploymentIds.clear();
long beforeCount = processDeployer.getDeployedCount();
logger.info("Number of Process Deployed Before start to deploy any new thing : " + beforeCount);
if(getBpmnFiles() != null)
for(String bpmn : getBpmnFiles())
{
if(bpmn != null)
deploymentIds.add(processDeployer.deploy(bpmn));
}
logger.info(processDeployer.getDeployedCount() + " Processes Deployed Successfully");
}
protected abstract String[] getBpmnFiles();
.
.
.
}
5. Simple Test Process Model
First of all we need a bpmn diagram you can find (SampleProcess.bpmn)
This process has a start event, one user task and an end event.
bpmn xml content is :
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1" targetNamespace="http://org.eclipse.bpmn2/default/process">
<bpmn2:process id="process_1" name="sampleProcess">
<bpmn2:startEvent id="StartEvent_1">
<bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:endEvent id="EndEvent_1">
<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="SequenceFlow_2" sourceRef="StartEvent_1" targetRef="UserTask_1"/>
<bpmn2:userTask id="UserTask_1" name="User Task 1">
<bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_3" sourceRef="UserTask_1" targetRef="EndEvent_1"/>
</bpmn2:process>
</bpmn2:definitions>
6. Run a Test
After all you can run a test like this :
public class SampleProcessTest extends BaseTest {
#Override
protected String[] getBpmnFiles() {
return new String[] {"com/test/activiti/sampleProcess/SampleProcess.bpmn"};
}
#Test
public void sampleFlowTest()
{
ProcessInstance pi = super.runtimeService.startProcessInstanceByKey("process_1");
Task ut1 = super.taskService.createTaskQuery()
.processInstanceId(pi.getId())
.active()
.singleResult();
assertNotNull(ut1);
super.taskService.complete(ut1.getId());
HistoricProcessInstance historicProcess = super.historyService.createHistoricProcessInstanceQuery()
.processInstanceId(pi.getId())
.finished()
.singleResult();
assertNotNull("Process must be finished",historicProcess);
}
}
7.Delegate Expression example
At the source link there is lots of example of DelegateExpression, but for now we create a sample BPMN process has two service tasks that define by DelegateExpression to Autowire with Spring bean :
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="awProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${awServiceTask1}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask2" name="Service Task" activiti:delegateExpression="${com.test.activiti.autowiredservicetask.AWServiceTask2}"></serviceTask>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
</process>
</definitions>
and AWServiceTask1 :
#Component("awServiceTask1")
public class AWServiceTask1 implements JavaDelegate {
Logger logger = Logger.getLogger(AWServiceTask1.class);
#Override
public void execute(DelegateExecution execution) throws Exception {
logger.info("Object Identity : " + this.toString());
}
#PostConstruct
public void init()
{
logger.info("awServiceTask1 is created");
}
}
and similar way is the code of AWServiceTask2, you can find the fully source at activiti-test.
and test class is :
public class TestAWServiceTask extends BaseTest {
Logger logger = Logger.getLogger(TestAWServiceTask.class);
public static String KEY = "awProcess";
#Test
public void autoWireTest()
{
processDeployer.printAllProcessDefinition();
processEngine.getProcessEngine().getRuntimeService().startProcessInstanceByKey(KEY);
}
#Override
protected String[] getBpmnFiles() {
return new String[] {
"com/test/activiti/autowiredservicetask/AutoWireServiceTask.bpmn"};
}
}
I hope you got your desired answer
My Spring RESTful web service is returning a JSON form of-
[{"key1":"value1","key2":"value2","key3":"value3"},{"key4":"value4","key5":"value5","key6":"value6"}]
Now when my spring MVC app, try to access it, to show in a JSP then Exception occurs saying-no suitable HttpMessageConverter found Please help me where I going wrong.Here is my code-
Inside #Controller class of my spring MVC app calling the RESTful service
//**com.songs.controllers.FrontSongController.java**
</*
author Rohit Tiwari
*/>
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
//SongResource.class is for representation on incoming JSON see below for its code
//This is the line no 48 that you will see in below browser logs
ResponseEntity<SongResource> listofallsongs=rt.exchange(url,HttpMethod.GET,entity, SongResource.class);
md.addAttribute("listname", "Songs available in the repository:");
System.out.println("Response Entity object= "+listofallsongs);
System.out.println("Response Entity body= "+listofallsongs.getBody().toString());
return "Sucess";
}
Inside config-servlet.xml of my spring MVC app calling the RESTful service
<context:component-scan base-package="com.songs.controllers" />
<mvc:annotation-driven />
<context:annotation-config/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Inside SongResource.java of my spring MVC app, which I am trying to use for converting the coming JSON to my SongResource.class object, that my spring MVC app can use in a jsp
//**com.songs.service.resource.SongResource.java**
public class SongResource
{
private String name;
private String film;
private String singer;
public SongResource(String name,String film,String singer)
{
this.name=name;
this.film=film;
this.singer=singer;
}
//setter & getters of all above
}
On calling the spring REST service from my spring MVC app the browser is saying as below-
Error 500--Internal Server Error
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.songs.service.resource.SongResource] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java :77)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:619)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
at com.songs.controllers.FrontSongController.getAllSongs(FrontSongController.java:48)
//and so on
Try this, hope it will help you
#RequestMapping(value="/alls",method=RequestMethod.POST)
public String getAllSongs(ModelMap md)
{
String url="http://localhost:7001/SongAppWS/songappWS/allsongsWS";
RestTemplate rt=new RestTemplate();
SongResource[] songRs = template.getForObject(url, SongResource[].class);
List<SongResource> songs = Arrays.asList(songRs);
md.addAttribute("listname", "Songs available in the repository:");
md.addAttribute("listValues", songs);
return "Sucess";
}
I am new in Spring test, and I have ran a lot of unit test successfully according to the documents step by step,however I have some questions:
1 Can all the TestCase use a global Spring Context
Now I configure each TeseCase the spring context like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class UserDaoTests {}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class AccoutDaoTests {}
As shown, spring will load and destroy the same context again and again.
So I wonder if I can setup a global Spring context, and then make all the TestCases run inside this context?
2 Transaction management
It said that the Transaction Manager will rollback the operations to the database.
But I have not found in which case this feature will work.
Because in my application I use ORMLite instead of Spring JDBC.
And this is the configuration:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- our daos -->
<bean id="ormliteSource" class="com.j256.ormlite.jdbc.DataSourceConnectionSource" init-method="initialize">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" value="${jdbc.url}"/>
</bean>
<bean id="userDao" class="com.j256.ormlite.spring.DaoFactory" factory-method="createDao">
<constructor-arg index="0" ref="ormliteSource"/>
<constructor-arg index="1" value="com.springapp.model.User"/>
</bean>
And then in my test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(..)
#Transactional
public class UserDaoTests {
#Autowired
private Dao<User, Long> userDao;
#Test
public void testCreate() {
User u = new User();
u.setName("ysl");
u.setLocked(true);
try {
userDao.create(u);
} catch (SQLException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
Then I run the test, and I found that all the tests are successfully passed, but when I check the database, I found that there are some test data inserted, it seems that the rollback does not work.
Do I miss anything?
Spring should detect if it can setup a shared application context for you. Did you specify different xmls in different test cases? If your test case could run against a shared application context, you may try with:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
public abstract class AbstractSpringTests
public class ConcreteTests1 extends AbstractSpringTests
public class ConcreteTests2 extends AbstractSpringTests
In this case, spring should setup only one application context.
According to the reference, you should add #TransactionConfiguration instead of #Transactinal on test class(rollback is the default strategy if I'm not mistaken)
If you want a transaction to commit — unusual, but occasionally useful when you want a particular
test to populate or modify the database — the TestContext framework can be instructed to cause the
transaction to commit instead of roll back via the #TransactionConfiguration and #Rollback
annotations.
I have written Webservice in java which has successfully created WSDL. I am stuck in writing a webservice client for my webservice in java. I would like to use my webservice from some jsp classes. How do i do it?
#WebService
public interface AddService {
double getMultipicationResult(double M1, double M2);
}
#WebService(endpointInterface = "com.sample.AddService")
public class AddServiceImpl implements AddService {
public AddServiceImpl() {
}
#Override
public double getMultipicationResult(double M1, double M2) {
M1 = M1*M2;
return M1;
}
}
I have written the client something like :-
public class AddServiceClient {
private AddServiceClient() {
}
public static void main(String args[]){
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringClientWebServices.xml"});
AddService client = (AddService)context.getBean("client");
double response = 0.0;
response = client.getMultipicationResult(10.0, 20.5);
}
}
and SpringClientWebServices.xml is as follows :-
<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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="client" class="com.sample.AddService"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.sample.AddService"/>
<property name="address" value="http://localhost:8080/sample/services/Addition"/>
</bean>
</beans>
I am getting exception as follows:-
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.cxf.jaxws.JaxWsProxyFactoryBean] for bean with name 'clientFactory' defined in class path resource [SpringClientWebServices.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.cxf.jaxws.JaxWsProxyFactoryBean
First of all you are missing the CXF jars as evident from the ClassNotFoundException. Please include the cxf jars.
Second regarding using the service in JSPs then you have to first initialize the Spring container via web.xml and not via main method. Use Spring MVC and implement controller which makes calls to webservice and provide data to the JSP.
If you want to consume a service directly from the JSP, consider a JavaScript client like mentioned: http://cxf.apache.org/docs/javascript-client-samples.html
I prefer to use a jar that contains the service interface and create a dynamic Spring client using CXF and Spring in a separate jar, then bring in both f those dependencies. This is also documented in the CXF site.
I need to configure the camel route endpoint using WSDL file.
I don't know the service classes and I don't want to put the service class files in my class path. I have only the WSDL file.
How can I solve my task ?
Define the camel cxf endpoint as follows
<cxf:cxfEndpoint id="testServiceEndpoint" address="http://localhost:9000/web-service/TestService" wsdlURL="TestService.wsdl" serviceClass="com.webservice.AllServiceService" endpointName="s:TestServiceHttpSoap11Endpoint" serviceName="s:TestPutService" xmlns:s="http://webservices/testService"/>
Route configuration
<route>
<from uri="cxf:bean:testServiceEndpoint"/>
<to uri="log:output?showAll=true" />
</route>
Note that I have mentioned a serviceClass attribute, but this class can be made generic to handle all webservices by using #WebServiceProvider annotation
#WebServiceProvider
#ServiceMode(Mode.PAYLOAD)
public class AllServiceService implements Provider<StreamSource> {
#Override
public StreamSource invoke(StreamSource request) {
}
}