0

I am quiet new in MVC and Spring and I want to learn best practices and solution so I decided to ask what is the best practice or how you would solve my problem.

I got controller with scope session (To store data in global variable not overrided when another user send request). I got global variable as I mentioned and two endpoints. First endpoint POST - here I send form and call another REST service to fetch data - call depends on data from form. Second endpoint GET - here i send which page to return. This endpoint is used for pagination.

Where is the problem? I have to store data in global variable because when I fetch data in POST endpoint I do not have access to it in GET endpoint. I do not like this solution. Do you have any ideas how to solve it better?

@Controller
@SessionScope
public class someController {
Global variable
    
@PostMapping(value = "/endpoint")
public String endpoint1(Form form){
    //here I fetch data from another REST service depends on form data and save it to global variable
 }
    
@GetMapping(value = "/anotherenpoint")
    public String endpoint1(int page){
    //here I get data from global variable and return to view as Page object
}
  • In almost all cases you _shouldn't do this_. Your `@PostMapping` is perfectly capable of returning a model-and-view rendering the results. – chrylis -cautiouslyoptimistic- Nov 10 '20 at 08:19
  • @chrylis-cautiouslyoptimistic you are right but only to page=0. How to paginate to POST endpoint? I will have to send form and page every time. – Lucky Luke Nov 10 '20 at 08:26
  • You need to provide a _lot_ more detail about your use case. In general, if you're performing a query (non-modifying), you should be using GET (even with a form). Note that Spring provides automatic support for binding `Page` to controller parameters, and you can pass that `Page` directly to Spring Data repositories. – chrylis -cautiouslyoptimistic- Nov 10 '20 at 08:32
  • ok first of all some business background. Polish goverment creates REST API to verify company. You have to search company by: taxpayer identification number, company account numbers, national business registry number. You can fill up to 30 numbers at time. For that I created form and send data via post. In POST endpoint I get data, verify if numbers are ok (via regex) and send call via goverment api (if user wants company details by account number I send that request if by other numer another request). I fetch data from request save it to gobal variable and return first result to view – Lucky Luke Nov 10 '20 at 08:50
  • As u may think it is not a good option for example to show 30 results at the same time so I created pagination. I send request to GET endpoint with infromation which page to get (page size is always the same 1 because I want to fetch 1 result at time). I return that result to view. Since this moment I will only use GET enpoint to paginate the results. I have to use global variable because I do not have access to fetchted data in POST endpoint. I can not reuse POST endpoint because I should send form every time and it is not good. Probably I designed something wrong I am still learning – Lucky Luke Nov 10 '20 at 08:55
  • Okay, I think I understand the general pattern of what you're looking for. In this case, you need to assign _the search itself_ some ID, save the results temporarily (in memory, in Redis, maybe in a database with expiration) and use [POST-redirect-GET](https://stackoverflow.com/questions/10827242/understanding-the-post-redirect-get-pattern) to send the user to the results page. – chrylis -cautiouslyoptimistic- Nov 10 '20 at 16:27
  • Yeah I think this is the best solution. Thank you – Lucky Luke Nov 11 '20 at 12:35

1 Answers1

0

Your @Controller shouldn't be session scoped, instead make your variable session scoped. (you can also annotate it with @Autowired, so your controller automatically has the right variable for the current user)

john_moo
  • 11
  • 2
  • ok and how about storing data in global variable? is it ok? – Lucky Luke Nov 10 '20 at 08:42
  • if you really need your data stored globally i would recommend using a [singleton](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes) – john_moo Nov 10 '20 at 11:35