vendredi 6 mars 2015

Spring RESTful Web service and bean "request" and "session" scope

I am using the pure example code of simple REST service from the spring guide as a base: http://ift.tt/Jb3BXl


I have added single Bean configuration:



@Configuration
public class Config {

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST)

public RequestData requestHelper() {
return new RequestData();
}

}


Then my modified controller looks as follows:



@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
System.out.println(applicationContext.getBean(RequestData.class));

return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}


and I am getting



java.lang.IllegalStateException: No Scope registered for scope 'session']


as the result of calling "/greeting"


I have read some description here: http://ift.tt/1KNLBhK however I am still confused.


they write: "The request, session, and global session scopes are only available if you use a web-aware Spring ApplicationContext implementation".


Does it mean that "AnnotationConfigApplicationContext" which I am using is not allowed in such case? Am I forced to use some xml configuration instead?


Aucun commentaire:

Enregistrer un commentaire