How to pass HTTP request to Web-Service - ajax

From my portlet JSP, I am trying to make a Ajax call to web-service.
My aim is to pass HTTP request object as a parameter to web-service method.
Please suggest.

Suggesting to rethink your problem. A servlet request object does not make any sense as parameter for a web service call. Those are two totally different frameworks. A servlet request only makes sense within the processing of a servlet and is defined within that context.
You are probably interested in some data from that object, e.g. attributes or some data from the session. Use exactly that data as parameters and forget about your original plans

Related

Where should I make API calls in a spring application, in the controller or service?

I have a spring application. It exposes an endpoint, which when hit, needs to make an call to some other API. So, where should I make this API call, inside my controller itself or should I do it in the service class?
Based on any Architectural style (DDD, Microservices, etc), we should follow separation of concerns.
Best practice would be to create a Rest Client class for the API you want to consume and make all rest calls inside that.
Then you create a Service class to call the method consuming the API, performing the operation, data filter, anything you want to do with data.
Next would be to inject your service class inside the controller and return the data you just consumed and did some operation on it.
It might not sound well in a small project/feature, but it is the best practice when things get complicated and grow.

Is it possible to create a proxy controller that ignores some part of the URI

I have a spring service with multiple controllers with following request mapping running on port 9000
/api/fruits
/api/vegetables
/api/plants
Given all these requests are being served at localhost:9000,is it possible via some Spring entity, that a request to localhost:9000/{Account_id}/api/fruits is routed to localhost:9000/api/fruits. I know the question is a little vague but I'm trying to see if I can add a proxy controller with mapping /{account_id}/ which ignores the rest of the uri and have some code there to forward it to the appropriate controller. Not sure if Spring interceptor or filter of some sort would be helpful here for me to modify the request and change the URI

Controller and Service Methods or Just Controller Alone

I come across the URL - https://dzone.com/articles/quick-guide-to-microservices-with-kubernetes-sprin where both the controller and the service methods are exposed as REST API's
The confusion comes and it araises the question whether we need to expose controller methods as REST APIs or Service methods are APIS
if we made both of them as REST APIs then when we call a controller methods - 2 HTTP request might be sent and will cause performance issue.
Please advise.
The example that you're looking at is Feign, it is a declarative webservice client to make webservice calls easier between multiple microservices. Feign comes with GetMapping because its designed to work with other microservices.
In General, Controllers will have RequestMapping and handle the routing and service will do the work of running your business logic.
If we configure 2 RequestMapping it doesn't mean 2 HTTP calls will be made, only the ones with right url will be invoked and the other one will be a simple spring bean.

Forward Spring HTTP Request

I have a restful web service written using Spring WebMVC that will mostly be used to orchestrate other services. In some cases these services are on the same server, in some cases they are not. I have a few requests (GET and POST) that will be direct pass throughs to another service. Is there a way to blindly forward all GET and POST data from a request for certain URLs without knowing anything about the data in the request?
Ideally, I would like to be able to say all requests for http://server1/myService/user/... should forward to http://server2/user/... with all of the GET and POST parameters forwarded with it.
For the services on the same server, if they're being served by the same Spring MVC application, you could use RedirectViews and/or the "redirect:" prefix.
For those on another server, the best thing I can think of would be to use a servlet filter, similar to the approach suggested by this post: spring mvc redirect path and all children to another domain

How to connect my Spring + Hibernate based application backend with pure HTML and AJAX based client?

I'd like to call methods of my DAOs by AJAX. I'm quite new in that so I would like to ask what is the best way to do that. Is it possible to publish my beans as web services and call them with e.g. jQuery? I think it is not possible :) I've also read about Direct Web Remoting but I don't know which way to go...
As I see, there are lot of experienced guys here so I think you can show me direction.. thanks in advance
Rather than exposing your DAO beans directly, you should create some Spring MVC controller beans, and call those from the client-side (using AJAX). Ideally, the controllers should not call the DAOs directly, but should instead call service beans (and the service beans should call the DAOs). One advantage of this approach is that you can define your service methods to be transactional, i.e. whenever a service method begins a transaction is started, and whenever a service method returns (without an exception) the transaction is committed. If the boundaries of your transactions are your DAO methods then it is not possible to wrap several database calls in a single transaction.
Of course there's no reason why you need to use Spring MVC - any web framework would suffice.
You have to expose your DAO's or beans by means of http. Typically you create a layer above the DAO layer to expose your services through HTTP, which are available to any AJAX framework such as jQuery. What jQuery and other frameworks ends up doing is using a special asynchronous request called XMLHttpRequest and then parse the server response (can be anything, pure HTML, JSON, XML, etc) and process it.
Here's a link I found that shows Spring & DWR with AJAX: Bram Smeets Blog.

Resources