0

I have a problem with spring boot. It's keeps data in a class that is annotated in:

@RestController, @Service, @Component.

I can't keep any data because it will be shared with others like a session.

Example:

@RestController
public class controller {

    int x = 65;

    @RequestMapping(value = "/set/{number}", method = RequestMethod.GET)
    public int view(@PathVariable("number") int number) {
        x = number;
        return x;
    }       

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public int view2() {
        return x;
    }
}

First call: localhost:80802/get Result: 65.

Second call: localhost:8080/set/5 Result: 5

Third Call: localhost:8080/get Result:5

Third call Result Supposed to be: 65

This situation is also happens in the other annotations that i mentioned.

Why its use @RestController class as a kind of singleton? I cant store anything in a class that is annotated?

thanks.

Furybone
  • 31
  • 5

1 Answers1

0

You can use @Scope annotation as described here. For example:

@Scope(value="session", proxyMode =ScopedProxyMode.TARGET_CLASS)
@RestController
public class controller {
....
}
vahidreza
  • 843
  • 1
  • 8
  • 19
  • I tried it , and it still keeps data in the "x" variable. I want that the data won't be stored in the class there in the next http call... – Furybone Jul 30 '16 at 14:26
  • I think, you must enable class proxies by adding `spring.aop.proxy-target-class=true` to `application.properties` file. – vahidreza Jul 30 '16 at 14:30
  • I've tried it also, and still, I don't get the initial number 65 in a "get" call, after a "set/5" call. – Furybone Jul 30 '16 at 14:36