I have two webs services or endpoints having one method each and each method is mapped with one URL. I am calling one webservice URL from REST client and in this method i want to call method in another web service which is mapped to URL. How can this be achieved in apache CXf ?
I tried using HttpClient to call another webservice from one but I am getting 404, if I use complete URL and getting 302 code but no response if I use relative URL. what might be issue and what is correct approach ?
You can try to call directly the other controller without httprequest.
for example you have the two next controllers with all annotations you need
#RestController
#RequestMapping("/a")
public class A{
#RequestMapping(...)
public void toCall(){
//your code
}
}
you want to call the method toCall of controller A from controller B
#RestController
#RequestMapping("/b")
public class B{
#RequestMapping(...)
public void method(){
A a = new A();
a.toCall();
}
}
Related
I'm using Resteasy with Quarkus (io.quarkus.quarkus-resteasy).
I have a path with params declared on a controller.
#RequestScoped
#Path("/v1/domain/{domain}/resource")
public class MyRestController {
#POST
#Consumes(APPLICATION_JSON)
public Response create(Entity entity) {
// here I create a new entity...
}
#GET
#Path("/{id}")
#Produces(MediaType.APPLICATION_JSON)
public Response get(#PathParam("id") String id) {
// get and return the entity...
}
}
I would like to retrieve the domain path param from outside this controller, in a provider marked with #Dependent for example, or in any interceptor that process the incoming request.
#Dependent
public class DomainProvider {
#Produces
#RequestScoped
public Domain domain() {
// retrieve the path param here !
}
}
I didn't find a way to do that, nor documentation about this.
I tried both:
injecting io.vertx.ext.web.RoutingContext with #Inject and access routingContext.pathParams()
using ResteasyProviderFactor to recover the request context data
In both case, there is no path parameter : the request path is resolved as a simple string, containing the actual URL the client used to contact my web service.
Edit:
As a workaround, in my DomainProvider class, I used the routingContext to retrieve the called URL and a regular expression to parse it and extract the domain.
There is no standard way to do this.
You need to pass the param from the JAX-RS resource on down to whatever piece of code needs it
I am working on Quarkus application and what I want to do is to set the global path from application.properties file for all rest rest, my application is working but while calling rest request it is giving not found 404.
#ApplicationScoped
public class ABC {
#POST
#javax.ws.rs.Path("/callit")
public Uni<Response> deleteNoti()
{
//whatever logic
}
}
#ApplicationScoped
public class PAR {
#POST
#javax.ws.rs.Path("/callitPar")
public Uni<Response> addNoti()
{
//whatever logic
}
}
And in application.properties file I am configuring below properties:
quarkus.resteasy.path=/rest/*
quarkus.rest.path=/rest/*
quarkus.http.root-path=/myapp
but when I am calling rest request from front-end it is not working, my rest request should be as below:
http://localhost:8080/myapp/rest/callit
http://localhost:8080/myapp/rest/callitPar
What I want is every rest request should start with "/rest/*" and my application base URL should be "/myapp", Let me know how can we achieve it?
Try to annotate your resource classes with #Path("/") and set quarkus.resteasy.path=/rest.
This should result in your described behaviour.
quarkus.rest.path can be removed.
I have a service using a method and I have actual method call also using it. can we differentiate between both in Aspect programming.
for eg.
public class AccountProcessorImpl implements AccountProcessor{
public Response calculateBalance(Account accountInfo){
//some implementaion
}
}
#Path("account")
public class AccountService{
#InjectParam
AccountProcessor accountProcessor;
public Response getBalance (Account accountInfo)
{
return accountProcessor.calculateBalance(accountInfo);
}
}
I have included the method calculateBalance in my Aspect program to do some authentication(PointCut()). Now I want to use this method just as a method call. Now the method fails due to the authentication. so Can I differentiate some how? like do authentication only if its a Rest API call and no need authentication if its a method call
Assuming you have layers (web, service, etc.), you could put security on the web layer and not the service layer.
I am using Jersey Restful webservices. I have below web method to get the results.
#Path("/persons")
public class PersonWS {
private final static Logger logger = LoggerFactory.getLogger(PersonWS.class);
#Autowired
private PersonService personService;
#GET
#Path("/{id}")
#Produces({MediaType.APPLICATION_XML})
public Person fetchPerson(#PathParam("id") Integer id) {
return personService.fetchPerson(id);
}
#DELETE
#Path("/{id}")
public void deletePerson(#PathParam("id") Integer id) {
return personService.deletePerson(id);
}
}
In above Jersey RESTful webservice, i have two web methods one for get and one more for delete with same number of parameters. In above case will there be any ambiguity? If not what should be the URIs for both of the methods? Thanks!
Thanks!
Jersey decides which method to call based on the HTTP method specified in the request. If you use multiple methods with the same HTTP method like GET, then the choice is made by more Annotations like Consumes or Produces etc.
BTW: If you use the URI /persons/{id} for all endpoints, then you can annotate your class with #Path("/persons/{id}") instead of annotating every method with this sub-URI.
There is no ambiguity as the HTTP Method is different (GET vs DELETE).
The same url would also be used to update the object, using the HTTP method PUT
No ambiguity since the HTTP methods used are different i.e GET and DELETE
And the urls will be same as the param required is "id" for both
IN Jersey client program use GET http method for fetching person info, Use DELETE http method for deleting the person.
I am writing a web service like
#Path("/pathName")
public class LoginServiceComponent {
#GET
#Path("/methodPathName/{param}")
#Produces(MediaType.TEXT_HTML)
public String getVoterByVoterId( #PathParam("param") String param)
{
.................
}
}
Here my url to access web service is http://www.abc.com/pathName/methodPathName/1
Here i have 10 methods.Is there any possibility to remove class level #Path means i have only one web service class in my project.So i dont want to use class level #Param repeatedly.
Thanks in advance...
If you want to avoid the #Path on the class so your URL's don't have the "pathName" in the path, I don't think you can remove the #Path on the class entirely. But I have used the #Path class annotation of #Path("/") and was able to get just URL to be just http://www.abc.com/methodPathName/1 (if that's what you're trying to do).