Here is a simplified ASP.NET Web API
public string Get() {
return $"Received value: 1,2 in string Get()";
}
public string Get([FromUri] int id) {
return $"Received value: {id} in Get(int id)";
}
How do I map these in WSO2 API Definition? I tried the below, but it does not seem to work.
I get the below error
{"fault":{"code":900906,"message":"No matching resource found in the API for the given request","description":"Access failure for API: /api/1.0, version: 1.0 status: (900906) - No matching resource found in the API for the given request. Check the API documentation and add a proper REST resource path to the invocation URL"}}
I would like the url to invoke something like this
http://localhost:8280/api/Default
http://localhost:8280/api/Default?id=123
Thanks in advance for your help!
You can define 2 resources for 2 cases. (i.e. for with and without query parameters)
In your URL, version is incorrect. It should be
http://localhost:8280/api/1.0.0
http://localhost:8280/api/1.0.0?id=123
If the version is the default version, you can simply drop the version like this.
http://localhost:8280/api
http://localhost:8280/api?id=123
Related
I am new to Enterprise Integrator so I may be facing a misunderstanding problem. I have installed WSO2 EI 6.1.1 on a Linux box. I have created a pass through proxy for a very simple form based web application and it works perfectly.
I want to filter the access to it by previoulsy validating a rest request to an external server. The JSON result of this external request should contain an specific value, f.e. {"valid":"yes"}.
I have created a Java class taht can make this call (at the present is just assigning statically the value and loading it in a variable):
public class validate extends AbstractMediator {
private String validated = "yes";
public boolean mediate(MessageContext context) {
System.out.println("Validating Access!!!!");
System.out.println("Validated: " + validated);
return true;
}
public String getValidated() {
return validated;
}
public void setValidated(String validated) {
this.validated = validated;
}
I see it running correctly.
As a second step I have created a Mediator filter with the aim of evaluating this value. I have tested this mediator with an always true xpath expresion 1 > 0 And I see that the then and else clauses work correctly.
I have not been able to check the value obtained in the first mediator in the filter mediator. How can I do this? What am I missing?
UPDATE:
I have found that the apparent way of solving the problem is defining a Property mediator to store the value. I have managed to use a property to make the decision in the filter but, how can I set that property with a value obtained in the Java class?
You just need to add this line in your java class : mc.setProperty("MyPropertyName", myValue);
In your mediation, use get-property("MyPropertyName");
I have recently used Java Spring to create REST services. In it, there were annotations for binding each specific function to a different REST query. Lets not go too technical here, as a psuedo, it was like this:
/** list records */
#bind("/list", WebMethod.GET)
List<Record> getRecords()
{
}
/** get record */
#bind("/record", WebMethod.GET)
Record getRecord()
{
}
/** add record */
#bind("/record", WebMethod.POST)
void addRecord()
{
}
Now I am given a tesk to perform a REST with Casablanca SDK in C++, but in every tutorial I checked covers a single GET or POST request. Is it possible to bind multpile requests to custom targets similar to Spring in Casablanca SDK?
You can make a single get function where you capture all get requests, then just get the path from the query and use a switch to call different functions to process the request for that path.
I need to version a REST web service API. I have gone through several posts under Stack Overflow. I discovered that there are three approaches: Url versioning, Custom Request header and Accept Header. I was about to go for URL versioning.
My URL is something like this:
http://localhost:8080/api/svcs/1.0/usrLocation?1234
Now my question how to version the above url. I didn't find any practical example from server side code. I was able to find the theoretical and URL like:
http://localhost:8081/api/svc/v1/ and blah blah..
How will my jax-rs implementation code will look like and how will the actual concept will be implemented in server side?
I am a beginner to this. My JAX-RS implementation is Apache CXF
My code:
#Get
#Path("/usrLocation")
#Produces (MediaType.APPLICATION_JSON)
public Response getUsrLocation(#QueryParam ("locId") String locId){
// logic
}
How the url http://localhost:8080/api/svcs/1.0/usrLocation?1234 needs to be changed to versioning so that it will be helpful in future and how it works from jax-rs implementation side?
You can follows two design approach
Say suppose your changes( would be) are minimal and its at particular part of code, for example you have insurance policy application and you are upgrading the application to new version because govt has introduced a policy which effects commissioning. and you have module for calculating commissioning then you can pass the version as a parameter.
#Path("/svc")
public class KPApiService {
private final static Logger LOG = LoggerFactory.getLogger(KPApiService.class);
#Path("/{version}/myapp")
#GET
public Response myApp(#PathParam("version")String version, #QueryParam("kpdata")String data){
LOG.debug("You and entered version {} and data {} ", version, data);
}
}
Say suppose you are changes are effecting so much and you want to introduce new module but there are few users(legacy) who are yet to brass the changes, then you can create a separate method with different version
#Path("/svc")
public class KPApiService {
private final static Logger LOG = LoggerFactory.getLogger(KPApiService.class);
#Path("/1.0/myapp1")
#GET
public Response myApp1(#QueryParam("kpdata")String data){
LOG.debug("You and entered version v1 and data {} ", data);
}
#Path("/1.2/myapp1")
#GET
public Response myApp2(#QueryParam("kpdata")String data){
LOG.debug("You and entered version v2 and data {} ", data);
}
}
The goal is to create a rest-full web service using JAX-RS that will selectively return the result either in json or xml format, depending on the user request. For example, if the user issues a GET request in the following fashion the results will be returned in json format:
http://api.myurl.com/order/1234.json
Similarly, if the user issues a get in the following request, the results will be returned in xml format:
http://api.myurl.com/order/123.xml
I don't want to use request parameters to do this (i.e. http://api.myurl.com/order/123?format=json). Using the .json or .xml post-fix seems more intuitive to me.
What would be the best strategy for doing this using the JAX-RS api?
One way would be to use #Path annotations more thoroughly:
#GET
#Path("/order/{id}.xml")
#Produces("application/xml")
public Order getOrderAsXML(#PathParam("id") int id) {
return realGetOrder(id);
}
#GET
#Path("/order/{id}.json")
#Produces("application/json")
public Order getOrderAsJSON(#PathParam("id") int id) {
return realGetOrder(id);
}
private Order realGetOrder(int id) {
// ...
}
However I'd be inclined to have a single method serving up both and let the client and supporting JAX-RS framework use content negotiation to decide the serialization method.
I am working on an ASMX web service; trying to create a method that will download a document from a server and show the document in the browser (the calling .aspx web page). My service builds without error but I get the following error when I try to "Add Web Reference" in my Proxy class project:
System.Web.HttpResponse cannot be serialized because it does not have a parameterless constructor.
Here is a snippet of the code in .ASMX file:
public class FileService : System.Web.Services.WebService
{
[WebMethod]
public void DownloadDocument(string URI, HttpResponse httpResponse)
{
int DownloadChunkSize = (int)Properties.Settings.Default.DownloadChunkSize;
// some more code here....
using (httpResponse.OutputStream)
{
// more code here...
}
}
}
I see I am confused about how to send back an HttpResponse from a web service to a requesting web page. Could someone please give me a tip on how to do this? Thanks.
You should look into web handlers (.ashx). They are perfect for what you are trying to achieve.
For example:
public class Download : IHttpHandler, IRequiresSessionState {
public void ProcessRequest(HttpContext context) {
var pdfBytes = /* load the file here */
context.Response.ContentType = #"Application/pdf";
context.Response.BinaryWrite(pdfBytes);
context.Response.End();
}
}
UPDATE:
An ashx handler is actually a replacement to aspx. Basically, it has no UI but still processes get / post requests just like an aspx page does. The point is to reduce the overhead generated by running a regular aspx page when all you need to do is return some simple content (like a file...) or perform a quick action.
The IRequiresSessionState interface allows you to use the SESSION object like any other page in your site can. If you don't need that, then leave it off.
This site has an interesting walk through on how to create one. Ignore Step 4 as you probably don't care about that.
Assuming that you have a regular page (aspx) that has a link to your document: The link in the aspx file would actually point directly to your ashx handler. for example:
Click Here
Then the code in the ProcessRequest method of the ashx handler would do whatever calls it needed (like talk to your DLL) to locate the document then stream it back to the browser through the context.Response.BinaryWrite method call.
That is not how standard ASMX web services work. If you want to make your own handler, or even use an ASPX page to deliver the doc, you are fine, but the standard ASMX web service method of doing this is to actually return the bits of the document as an encoded blob.
If you want to roll your own, consider this article:
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
The web smethod (from asmx) returns an object, which can be serialized.
You need to create your method like:
[WbeMethod]
public byte[] DownloadDocument(string URI)
Or if the content is some text - return string.