How to send HTTP post request using spring - spring

What configuration do i need to send HTTP post request through spring. I am using java application , it's not a web project. Can i use spring to send HTTP post request? I google it but most almost all examples are using spring MVC. Can i use just spring to send HTTP post request ?
I found this bean on net but I don't know what to do after it. I am using spring3.2 and this post i think is of year 2008...
<bean id="httpClient" class="org.springbyexample.httpclient.HttpClientTemplate">
<property name="defaultUri">
<value><![CDATA[http://localhost:8093/test]]></value>
</property>
</bean>
Any suggestions?

If you are using Spring 3.0+, it will be better to use RestTemplate for sending HTTP requests. Once you wire the RestTemplate (API), you can use different methods in it to send different types of HTTP requests.

You don't need Spring just to issue a HTTP post, see this post: Using java.net.URLConnection to fire and handle HTTP requests
And yes you can use Spring in a non-web application / command line application. Just create an instance of ApplicationContext (eg: ClassPathApplicationContext with path to your beans xml configuration injected)

Related

Spring MVC: log all requests, even the resource not founds

How can I log all requests in Spring MVC, even the resource not founds ones?
I guess interceptors can not log resource not found requests. What do you suggest?
Have you tried using implementation of javax.servlet.Filter?
Filter in contrary to Spring's interceptor is part of a Java's standard and is executed by the servlet container for each incoming HTTP request..
Spring MVC use exactly one servlet and that is DispatcherServlet which, as the name suggest, disptach received request to correct controller which process the request futher.
Spring even provide few concrete implementations of Filter that can log incoming request for you such as: CommonsRequestLoggingFilter or ServletContextRequestLoggingFilter.
You can choose one of the above implementations or implement Filter interface by yourself - whatever you decide, by using Filter you should be able to log every single request received by your servlet container.

Start with Apache Camel

I’m very new to Apache Camel and will very appreciate if someone could provide me what camel components may be used to solve particular task.
I have a simple REST WS. This service is not accessible to audience.
The idea is to build middle layer between user requests and endpoint service.
So I will have to catch user’s request, make some manipulations with it, send to restricted WS and give a response to user.
I’m just started learning apache camel and the question is what is the best way to implement this logic.
Thx in advance!
Frankly, Camel is not the right framework to implement web controllers. Of course there is the Camel Rest Module, but it is stretching the responsibilities of the framework too far.
I recommend using a more adapt framework for the implementation of the WS, e.g. Spring or Jersey, and call Camel endpoints programmatically from the request handlers. Within Spring, triggering Camel Endpoints is easy since the CamelContext can get autowired into your web controller:
camelContext.createProducerTemplate().sendBodyAndHeader("direct:myEndpoint", null, "id", id);
For your Camel Root this approach means, that it starts of with a Direct endpoint, then forwards to a Camel http endpoint and if necessary forwards the output from the HTTP call to some Spring bean transformation step, before finally passing it back to the web controller handler method:
<route>
<from uri="direct:myEndpoint"/>
<to uri="http:somehost.com"/>
<transform>
<method ref="springBean" method="doSomeTransformation"/>
</transform>
</route>
Well there are several camel components you can use for this task. Think of Camel as a toolbox where you can choose from several tools for the same task.
You can use:
Camel-HTTP4 http://camel.apache.org/http4.html
Camel-Jetty http://camel.apache.org/jetty.html
Camel-Restlet http://camel.apache.org/restlet.html
Camel-CXFRS http://camel.apache.org/cxfrs.html
Example using java dsl:
from("jetty://http://localhost:7070/test").to("jetty://http://localhost:7070/test1");
Example using blueprint
<route>
<from uri="jetty://http://localhost:7070/test"/>
<to uri="jetty://http://localhost:7070/test1"/>
<route>

Camel SOAP CXF Call With Dynamic Addresses

I'm trying to write a SOAP Web Service that:
accepts one request type A
maps request A to another outbound request type B
sends request B to an external SOAP service
maps the response B back to a response A object (and returns it)
I have this working when the endpoint (B) is statically configured.
But I want to be able to hit a variety of services with varying request/response types. These would probably be configured via a properties file.
Is it possible to do this in some generic/dynamic way?
Here's my spring camel XML:
<!— SOAP inbound service —>
<cxf:cxfEndpoint
id="paymentService_A"
serviceClass="#paymentServiceBean"
address="/PaymentService"/>
<!— SOAP outbound service —>
<cxf:cxfEndpoint
id=“paymentService_B"
wsdlURL="http://localhost:9080/externalpayment/ExternalPaymentService?wsdl"
serviceClass="com.yngwietiger.ExternalPayment"
address="http://localhost:9080/externalpayment/ExternalPaymentService"/>
<!— MAP from inbound SOAP request object to external SOAP request object —>
<bean id="mapAToB_RequestProcessor" class="com.yngwietiger.MyProcessor"/>
<!— MAP external SOAP response to a response for the initial/inbound SOAP request —>
<bean id="mapBToA_ResponseProcessor" class="com.yngwietiger.MyPostProcessor"/>
<camel:camelContext id="camelContext">
<camel:route>
<camel:from uri="cxf:bean:paymentService_A"/>
<camel:process ref="mapAToB_RequestProcessor"/>
<camel:to uri="cxf:bean:paymentService_B"/>
<camel:process ref="mapBToA_ResponseProcessor"/>
</camel:route>
</camel:camelContext>
Obviously, I'm using Camel's cxfEndpoint bean. But I don't see any way to set the address, wsdlURL, etc for each request. Is that possible?
Or am I going to have to build a route for each type? If so, how do I build one of these cxfEndpoints dynamically?
Would using Spring's WS Template be more flexible?
Is there a better way that I should be doing this?
Thanks in advance.
Camel Recipient List would better fit into your requirement. This is the link, http://camel.apache.org/recipient-list.html. You have to generate the dynamic endpoint and set into the header somewhere in the route and call the recipient list.
I think you can use HTTP endpoint for your outbound message.
As it is done in the example here

Interceptor for http outbound gateway

Has sprint integration http outbound gateway has any configuration property to add interceptor into org.springframework.web.client.RestTemplate ?
I want to intercept the httpRequest for adding some security information in request (like interceptor property in ws-ouboundgatewy) ,But I could not see any interceptor configuration option in http outbound gateway?.
Do we have any other option to achieve this functionality ?
You can inject to the <int-http:outbound-gateway> any custom RestTemplate bean using rest-template attribute on the first one.
But from other side I don't see any mentions for interceptor logic in the RestTemplate...

How to provide custom soap fault message if endpoint is wrong or invalid in soap request in spring integration

I am working in spring integration soap web service module for my project requirement. In my project if soap request does not find endpoint then there is a need to send soap fault message like "Invalid endpoint", for example if end point to access my service is http://www.mycomp.com/mychannel in request but user sends in request http://www.mycomp.com/myproject, here I want to send response as "invalid endpoint" soap fault.How can I achieve this in spring integration.
please find below ws-conf.xml
<context:component-scan base-package="com.mycomp.mychannel" />
<context:spring-configured/>
<context:annotation-config/>
<import resource="classpath:/WEB-INF/soap/config/g-config.xml" />
<!-- this is used for the endpoint mapping for soap request -->
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" />
Thanks in advance.
Your question isn't valid or isn't full. Or I just don't understand you.
Actually MessageDispatcherServlet (Spring WS) just delegates request to the SoapMessageDispatcher, which ends up with NoEndpointFoundException, if there is no Endpoint with the provided path in Request.
And that Exception will be treated as SOAPFault with the faultMessage like No endpoint can be found for request ....
So, be more specific, please. Looks like Spring WS already has all required features.

Resources