How to call get and post services from spring through apigee

plvndinesh
Participant II

Hi,

I have written a rest controlller which has two methods with same request mapping name

(/getEmplyeeDetails).

@RequestMapping("/api/emplyee")
@RestController
public class EmployeeMasterController {

@RequestMapping(value="/getEmplyeeDetails", method = RequestMethod.GET)
public @ResponseBody String getEmployeeDetails() {

}

@RequestMapping(value="/getEmplyeeDetails", method = RequestMethod.POST)
public @ResponseBody String getAllEmployee(@RequestBody EmployeeDetails empDet) {

}
}

I need to call the service by configuration the url in targetConnection but how can i differentiate between post and get call.

<HTTPTargetConnection>
<Properties/>
<URL>http://xxxxx.yyy/api/emplyee/getEmplyeeDetails</URL>
</HTTPTargetConnection>

0 1 762
1 REPLY 1

amitkumar2091
Participant III

By default apigee uses proxy's request verb to hit the target .

So if your apigee proxy is just a pass through then you can simply hit apigee url with GET and POST (please include content-type header with POST) verb and you will be able to hit both request mapping in spring.

Verb can also be modified within apigee flow conditionally by changing the value of variable request.verb , you can do it either through assign message or JS policy easily.

 <AssignMessage name="AssignMessage">
  <AssignTo createNew="true" type="request">request</AssignTo>
    <AssignVariable>
      <Name>request.verb</Name>
      <Value>POST</Value>
    </AssignVariable>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>
context.setVariable("request.verb","POST");

For mentioned scenario its better to create conditional flows to apply different policy's for each verb , for example you might require JSON validation or threat protection with post.

<Flows>
        <Flow name="GET_EmployeeDetails">
            <Description/>
            <Request>
            </Request>
            <Response>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/getEmplyeeDetails") and (request.verb = "GET")</Condition>
        </Flow>
        <Flow name="POST_EmployeeDetails">
            <Description/>
            <Request>
            </Request>
            <Response>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/getEmplyeeDetails") and (request.verb = "POST")</Condition>
        </Flow>
</Flows>

Hope this will help !!!