Middleware in Play framework (Java) [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have experience web programing using express js framework
and this is my first time to learn playframework in java
how to make middleware in play framework
as ussualy i use express js just add middleware in front of controller in routes
example like this
router.get('/all/:key' , user_mid.login, ctrl_post.all)
user_mid.login = is my middleware
ctrl_post.all = is my controller method to handle the request
so how to make middleware in play framework

Play does not have a concept called middleware.
In Play Java, Results are returned by classes extending the Action (or Controller) abstract class. Why am I telling you this? Well, because you can compose actions. One action can forward a request to the next action, then to the next, and the other way around with the response. Play has good support for this concept: Action composition .
This goes in three steps:
Define an action by extending (usually) play.mvc.Action.Simple .
Annotate the method in your controller with #With, and your newly crated class. That way you composed two actions. They are called in the order in which the #With annotations appear, if you have multiple.
(Optionally) Define your own annotation for an action.

Related

I want to use Spring integration in my project, please help me with the pattern that I should go with as per my requirement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I want to implement spring integration for my current requirement. The requirement is as follows -
I have a service and I need to call 3 services parallelly from it.
All the 3 calls are independent calls.
Out of the 3 calls 2 calls are HTTP calls(1 GET, 1 POST) and another is a native method call.
Once I make a POST call to my service then internally the three services should be called parallelly.
The request body that I'm using to do the POST call should be used as an input for the other 3 calls but I need to change the input JSONs as per need. The exact input I'm not going to be using for the other 3 services but I'll be modifying the input as per my need.
In the end once I get the response from the 3 mentioned services then I'll use a method to aggregate the JSONs and will use that Aggregated and transformed JSON as an input for another new service's input.
Kindly suggest me a correct pattern and with a little elaboration if possible as I'm totally new to Spring Integration.
So, to make a 3 parallel calls for the same payload and then aggregate their result, you need to look into a Scatter-Gather pattern: https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#scatter-gather.
According to your description, you have to use a configuration based on the PublishSubscribeChannel (Auction) with a TaskExecutor provided. The subscribers to this channel could be plain HTTP message handler, service activator or even transformers to manipulate a payload before target service call.

The model in Spring MVC [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
You know that in spring you can have a method that gets a model as its parameter. I am new to spring and I don't understand where does that parameter come from. Is is some kind of spring default bean or what?
You can check this article!
Generally - as always it depends on your implementation. As #Aniket Warey said Model object can be passed from view - JSP or HTML files.
You have also something like ModelAndView, which works slighthy different from ordinary Model. You can also specify and configurate/implement your own ViewResolver. Spring/Spring Boot uses its own default implementation for Model, but like I said - you can override it.
Leaving the subject a little bit - you don't usually need to use Model parameter. It always depends on problem that you face, but there is for example #RestController, which can get/send you data as JSON. Then you can neatly manipulate it on the client/view side. There is a lot of options. :)

Where should I perform Retrofit operations? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to MVP, Retrofit and Dagger, so I've decided to make a simple app based on Riot Games API, that just shows some game's info in a list. Everything works fine, but it made me wonder: "Is presenter a good place to do such things, like Retrofit operations? I couldn't find anything about it. There are some simple examples how to use Retrofit, but they are sometimes shown even inside an activity class, which doesn't seem right to me.
So, here is my question: Where should I perform such operations to make the code as clean as possible?
My app repository: https://github.com/Mallorax/Rito_Api_Test
If the Model is what Retrofit returns, you can't put the call there.
The View is intended to separated from the Presenter, and subscribes via Callback interfaces, which Retrofit provides out of the box.
Therefore, your only option without involving some Service layer is to put Retrofit in the Presenter.
I suggest you to place some network call to plain class e.g. Interactors for example LoginInteractor which contains loginMethod, than when it finishes pass the result to the presenter class, which will handle the data and will convert (or not) it for some kind of model which is ready to be presented on UI.
Activity/fragment contains Presenter and Presenter contains Interactor(s)

How mocks are created [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My question is how mocks objects are created not how to create a mock object using a library.
I have looked at the Mockito library source code but I didn't understand how its done. I have searched in the Internet but the articles explain what are mock object and how to create them using libraries.
For dynamic programming language perhaps it's simple as we can change methods, variable but how its done in static programming language (Java for example)
Let's begin with what a mock is: an object that you can set expectancies on it regarding methods that expects to be called, and/or parameters on those methods and/or count of calls on those methods.
Mocks are sent to tested objects in order to mimic certain dependencies without having to use the real code (in many cases this is problematic/dangerous, like dealing with payment gateways).
Since mocks will need to intercept calls to all (or some, in case of partial mocks) methods, there are several ways they can be implemented, depending mainly on the features the language provides. Particularly in Java this can be implemented via proxy classes: https://stackoverflow.com/a/1082869/1974224, an approach that kinda forces you (but in a good way) to use interfaces in your code when relying on dependencies.

Implementing a public API [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Soon I will have to implement a public API (web API). One of the requests will be that we will need to provide different methods to do the same (for example, REST and SOAP), my initial approach is as this.
Make a Controller for the REST
Make a Controller for the SOAP
Use a common Facade class that will handle requests from the REST and the SOAP interface, the Facade class will be in charge of accessing the internals of the system (directly or indirectly, dome DAOs could be used...etc).
Code will be written in Java, most likely will use Spring-MVC among other techs.
Question here is. Does this make sense to you? Is it too absurd? Do you feel any over-architecture here? Any suggestions/experiences/best practices?
Thanks for your time :)
Your approach makes sense to me. Spring MVC on its own can handle the REST endpoints, accepting and returning XML and/or JSON. For SOAP, you will need an additional framework like Spring-WS or Apache CXF.
When using the MVC pattern, it is always best to keep your Controllers as small as possible, by delegating any real work to other beans injected into the controller. These other beans often follow the Service or Repository sterotypes (using Domain Driven Design terminology). These Service and Repository beans can be re-used across REST and SOAP endpoints. I think this is what you mean in your third paragraph, if that is the case then you are on the right track.
Does this make sense to you?
Yes.
Is it too absurd?
No.
Do you feel any over-architecture here?
No.
Any suggestions/experiences/best practizes?
You can find design patterns describing what you are doing. For instance, the GoF adapter pattern says: "In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface." You have the App Controller (adaptee) that it is specialized (adaptor) in two interfaces: REST and SOAP.

Categories