The model in Spring MVC [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 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. :)

Related

Rest Api design: resource archetypes, store and controller [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 2 years ago.
Improve this question
i am currently in the process of looking more closely at the design of truly restful services.During my research I read something about resource archetypes at rest, four different types of archetypes come up frequently in discussion: document, collection, memory and controller. The first two I understand without any problems, but the last two I don't really understand. Can someone please give a clear explanation and example for both? Thank you.
Sure, there are four main archetypes at rest, the two that you're asking are:
Store: It'll never generate a new URI, we'll use the next URI http://api.example.com/song-management/users/{id}/playlists a user can PUT, GET and DELETE any playlist from its account but this store, the store is always managed by the client.
Controller: Are like functions, when HTTP verbs can't say the action which a resource will do, you should use this archetype. Eg. http://api.example.com/song-management/users/{id}/playlist/play will starts a playlist.
resource: https://restfulapi.net/resource-naming/

What's the purpose of objects in Java? [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
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system

Mapping a java Entity to a GraphQL object [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 3 years ago.
Improve this question
I have difficulty in implementing GraphQL in a java project as a part of updating it. I'm trying to connect an entity (which uses the Hibernate ORM to map to different databases) to a GraphQLObjectType . Any suggestions how can I accomplish this? Can I omit the GraphQL database configurations if so?
There're multiple options here.
It's probably best to not even map an entity directly . Entities are direct representations of the DB and, as such, should probably not be directly exposed, but wrapped into DTOs (maybe allowing pagination, flattening relations, or whatever is appropriate) instead.
If you just need to map the class (entity or not) to a GraphQLObjectType, graphql-java-annotations is the simplest (and most limited) route (check the status of this project first, it was on a hiatus for a while)
If you want to expose the entire entity graph through GraphQL, graphql-jpa might be your best bet (as Sriram suggests), as it's intended to do exactly that, while also adding pagination, aggregation and sorting
If you want to automatically expose not only an entity/DTO class, but also the operations upon it (e.g. an arbitrary service class), look at graphql-spqr (I'm the author of that project)
If you already defined your entities using JPA/Hibernate, try this:
https://github.com/jcrygier/graphql-jpa

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.

Understanding Getter() and Setter() in Struts [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
How to invoke getter() and setter() methods in struts?
Your question is rather vague. But the typical scenario in Struts2 is: you have an action with some properties which follow the Java bean conventions ( say, a 'myval' property is accesible via getMyval() and setMyval() public methods).
When invoking the action, the default configuration (with default interceptor stack) maps the http parameters calling the setter. Ej, if you call http:/..../myAction.action?myval=xx Struts2 will instance your action and call the method setMyval("xx") (if your property is not a string, struts2 will try to convert it).
After the action execution, when the results are displayed in the view (say,a JSP page), you might write <s:property value='myval' /> and Struts2 will invoke the method getMyval() of your action.
This is the most basic and typical workflow, but I'm simplyfing, everything is much more general and customizable.
See this article regarding general accessors and mutators in Java, and this one for accessors and mutators as specifically applicable to JSP's.

Categories