Paypal adaptive API and Spring 3 rest template — Part 2
So wellcome to part 2
This is where we start getting into the meat of the implementation. Assume you have set up the project described from part 1, this part will focus on getting the request done.
All the code describe here can be found at my github account. So I wont write everything here and you can check out the code to work that out. Hopefully I have made the code descriptive enough to follow
if not you can sent it to here
Set up your rest template definitions:
Add this to your existing definition. The 2 convertors here are very useful. This tells spring to either use Jaxb2 or String when unmarshalling the data. Spring rest template will find the best fit for your needs.. this is very cool as it save loads of if statements and therefore some tests too
In part 3 this is also where we will add our custom converter definition:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</list>
</property>
</bean>
Go back to MainPost class:
Add the following dependancy from RestTemplate
@Autowired
private RestOperations restOperations;
Then in makeRequest() you need to use the postForEntity method. This is because you need to set some custom headers for paypal and the only way you can do this is to create a HttpEntity yourself. Have a look at the generateHttpHeader() in the code. What you want at the end of a successful invocation is to get a paykey from paypal so you can finally construct a URL to pass back to the browser for a redirect. From here also take a look at the DSOs for mapping the request to be send
On thing to note if the request fail it throws a HttpStatusCodeException Runtime exception. This is different to how you will be use to if you used apache httpClient directly, even tho I am not sure I totally agree how spring deal with this but is good to know nonetheless.
Stay tune for part 3 where we will work with rest templates HttpConvertor

One Comment
Leave a CommentTrackbacks