I have a requirement to send dynamic query parameters to REST web service GET method [as shown below].
host:port/app?field1=XXX&value1=VVV&field2=XXX&value2=XXX ....
The consumer can send parameters up to fieldn and valuen. Each field maps to the value.
With this type of requirement, I can't code a finite set of QueryParams on the server side method.
Is there any type of REST library that supports this? I checked RESTEasy and Jersey, and they both don't seem to support this [as far as I checked].
Thanks.
Use UriInfo.getQueryParameters(), as following:
#GET
#Path("/foo")
#Produces(MediaType.APPLICATION_JSON)
public Response foo(#Context UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
...
}
It returns a MultivaluedMap. Then just iterate over it.
Related
For my POC, created simple lambda function , which will give emp information through rest api.
Created lambda function and access all the emp data using API gateway.
Facing some challenges while accessing particular data.
i am looking for
emp/1 - to retrieve emp id
emp/_search?name="apple" - search name contains apple.
Question is how to retrieve path and request parameters in java code.
public class TestAwsLambdaFunction implements RequestHandler<Map<String, Object>, String> {
#Override
public String handleRequest(Map<String, Object> input, Context context) {
String empID= null;
try {
#SuppressWarnings("unchecked")
Map<String, String> pathParameters = (Map<String, String>) input.get("querystring");
empID= pathParameters.get("id");
System.out.println(empID);
// TO-Do Business logic -
} catch (Exception e) {
// TODO: handle exception
}
return "Hello from Lambda!" + empID;
}
}
What is the best way to expose my data in Rest api call. Bit confused with Lambda or serverless .
have any option to show the data via page wise. Since i am new to AWS. Please guide me
Question is how to retrieve path and request parameters in java code.
You can use mapping template to send $input.params('name') property in the request body to your Lambda function.
What is the best way to expose my data in Rest api call
Use the proxy integration with these guidelines:
Avoid greedy path variables, except perhaps for a catch-all 404.
Avoid using the ANY method.
Define request models and enable request validation (remember it’s off by default).
In your Lambda, check that the content-type header matches one of your request models, and return a 415 Unsupported Media Type status code if it doesn’t (the proxy integration uses the WHEN_NO_MATCH passthrough behavior). After this check, your Lambda can assume the request validation is fully enforced.
By Ben Kehoe
https://read.acloud.guru/how-you-should-and-should-not-use-the-api-gateway-proxy-integration-f9e35479b993
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-java
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
https://github.com/vaquarkhan/Serverless-AWS-Lambda-guide/blob/master/01-aws-lambda-serverless-framework/02-building-rest-api-in-nodejs-with-lambda-gateway.md
https://www.puresec.io/blog/aws-security-best-practices-for-api-gateway
https://www.stackery.io/blog/serverless-function-architecture-principles/
https://technology.finra.org/code/enjoying-auto-scaling-integrated-authentication-low-host-cost.html
You need to choose Lambda Proxy Integrations when you set up your API Gateway. Here's official document Set up Lambda Proxy Integrations in API Gateway.
In this case, API Gateway will pass the whole request data to Lambda, including the request headers, query string parameters, URL path variables and so on. Then you can parse the data using your Java code.
Can cookies be used with ember-network requests? Thanks to this answer I know that they can be used with ember-data API requests, but I need to do a network request in an initializer and it doesn't appear the ember-data store can be accessed that early.
Background:
I'm wanting to persist shopping cart data to the backend for these reasons
The ember-cart addon has a smart way of persisting the cart by jsonifying and data model and dumping to localstore when it changes:
window.localStorage.setItem('cart', JSON.stringify(this.payload()));
then upon return visit parsing the json and pushing it into the store in an instance initializer:
...
payload = JSON.parse(payload);
...
cart.pushPayload(payload);
I'd like to do basically the same thing, but instead of getting the JSON from localstorage, get it from the API via the network.
the store ins't available in an initializer, but ember-network is. So hypothetically I think I can do this. The problem I'm running into is that the cookie isn't being passed.
I get around this with ember-data by using this:
xhrFields: {
withCredentials: true
}
in the application adapter, but I can't find any info about whether there's a similar setting for ember-network. I see the request to my API being made in the initializer, but the api doesn't return anything because the browser cookie isn't included.
The fetch API provides a credentials option..
This is also documented at the whatwg-fetch library used by ember-network.
So basically you can do
fetch("/foobar", { credentials:"include" }).then(...)
I am creating SOAP web service using Spring Boot SOAP Webservice Sample project. If I use following code dynamically generated WSDL shows Operations.
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "AvailNotifRequest")
#ResponsePayload
public OTAHotelAvailNotifRS getAvailNotif(#RequestPayload AvailNotifRequest request) {
But I need request element to change like this.
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "OTAHotelAvailNotifRQ")
#ResponsePayload
public OTAHotelAvailNotifRS getOTAHotelAvailNotifRQ(#RequestPayload OTAHotelAvailNotifRQ request) {
I found a similar question on this link Spring web service dynamic wsdl not generating message for a schema element answer says we need to add suffix Request after request element like AvailNotifRequest but I want to use OTAHotelAvailNotifRQ as my request input.
How can I use this because I am not getting operations in wsdl when I change request input like this.
According to official Spring-WS documentation:
The <dynamic-wsdl> builds a WSDL from a XSD schema by using conventions. It iterates over all element elements found in the schema, and creates a message for all elements. Next, it creates WSDL operation for all messages that end with the defined request or response suffix. The default request suffix is Request; the default response suffix is Response, though these can be changed by setting the requestSuffix and responseSuffix attributes on <dynamic-wsdl />, respectively.
In other words you can use the setRequestSuffix and setResponseSuffix on DefaultWsdl11Definition in order to specify a request and response suffix different from the default one. In the above case that could for example be:
wsdl11Definition.setRequestSuffix("RQ");
wsdl11Definition.setResponseSuffix("RS");
I'm using WSO2 API Manager and I want access to details of the client's incoming API request (into API Manager, for example, the HTTP method) as well the response from my API endpoint. I've followed the approach in the following document to write a custom mediator class which gets invokes on both the "In" (for the request) and "Out" (for the response) flows:
https://docs.wso2.org/display/AM160/Adding+a+Mediation+Extension
It seems I can get various bits of data that I need from the MessageContext that is passed into my mediator, but I'm struggling with getting the response code from my API endpoint. Is there a way to get access to the HTTP response itself (and all it's headers and other elements) from the MessageContext? I stumbled across the PassThroughTransportUtils class which has a determineHttpStatusCode method which I could call but I'm not sure this is the best way of doing it.
"HTTP_SC" property is stored in Axis2MessageContext, which can be accessed as below:
org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
String httpStatusCode = (String) msgContext.getProperty(NhttpConstants.HTTP_SC);
can be null, if not set.
I'm developing SOAP web service using Apache CXF framework. My web-method returns either binary data or plain XML depending on request parameters. Most of requests return binary data, so I configured CXF to use MTOM in service responses.
But this is not always useful: when XML is returned, caller side expects to get plain text/xml document rather than multipart one. So I'd like my web service to dynamically change its binding.
CXF documentation has following example:
Endpoint ep = ...; // example does not explain how to get it
SOAPBinding binding = (SOAPBinding)ep.getBinding();
binding.setMTOMEnabled(true); // or false
Question: how can I get Endpoint instance?
I'm using Spring annotation #Endpoint for web-service and #PayloadRoot for web-method.
You can use the following code if you are using on server,
you need to add import javax.xml.ws.Endpoint;
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);
From client side
TestMtomService tms = new TestMtomService(wsdlURL, SERVICE_NAME);
TestMtomPortType port = (TestMtomPortType)tms.getPort(PORT_NAME,TestMtomPortType.class);
Binding binding = ((BindingProvider)port).getBinding();
((SOAPBinding)binding).setMTOMEnabled(true);
Refer
If you are downloaded the cxf bundle, code samples for MTOM Server/Client available on following path
apache-cxf-2.7.2\samples\mtom
I created my own marshalled class extended from org.springframework.oxm.jaxb.Jaxb2Marshaller. Only single method is overriden:
public class Marshaller extends Jaxb2Marshaller {
#Override
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException {
if ( disableMtom() ) {
super.marshal(graph, result, null);
} else {
super.marshal(graph, result, mimeContainer);
}
}
private boolean disableMtom() {
return ... // depends on response context
}
}
The disableMtom detects if MTOM is disabled from response context. Web service endpoint takes care to pass this context to marshaller somehow.
By default MTOM is enabled.