Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: injecting @RequestBody into @Bean

I want to populate requestScopedBean.userDetails when my controller's update(...) gets executed / before it is executed.

In my spring web project I have java based MyConfiguration extends WebMvcConfigurerAdapter in which I have:

@Bean(name = "requestScopedBean")
@Scope(value = "prototype")
public RequestScopedBean requestScopedBean() {
    return new RequestScopedBean();
}

While in RequestScopedBean.java :

public class RequestScopedBean {

    public @Autowired UserDetails userDetails;

    public void setUserDetails(UserDetails pUserDetails){
        userDetails = pUserDetails;
    }

    @Override
    public String toString() {
        return "" + (userDetails != null) ;
    }
}

And UserDetails.java

public class UserDetails {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And I have UsersController.java containing :

@RestController
@RequestMapping("/users")
@Scope("request")
public class UsersController {

    @Autowired
    private RequestScopedBean requestScopedBean;

     @RequestMapping(
         value = {"{uid}" }, 
         method = RequestMethod.PUT, 
         produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<?> update(
        @PathVariable String uid,
        @RequestBody UserDetails pUserDetails
    ) throws Exception {

        // LOCATION 1

        // return ResponseEntity;
    }
}

What can I do such that at LOCATION 1, requestScopedBean.userDetails holds variable pUserDetails received in this functions as parameter.

I am looking for some setter-based dependency injection or some factory method based injection though please do advise if some other method is more suitable. Thanks

like image 679
zur Avatar asked Nov 08 '22 03:11

zur


1 Answers

If you just want to keep the UserDetails object which came from Request, just set the object to requestScopedBean.

@RestController
@RequestMapping("/users")
@Scope("request")
public class UsersController {

    @Autowired
    private RequestScopedBean requestScopedBean;

     @RequestMapping(
         value = {"{uid}" }, 
         method = RequestMethod.PUT, 
         produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<?> update(
        @PathVariable String uid,
        @RequestBody UserDetails pUserDetails
    ) throws Exception {

        requestScopedBean.setUserDetails(pUserDetails);

        // return ResponseEntity;
    }
}

Apart from this, I advice you not to Autowire in your UserDetails object in RequestScopedBean

public class RequestScopedBean {

    public UserDetails userDetails;

    public void setUserDetails(UserDetails pUserDetails){
        userDetails = pUserDetails;
    }

    @Override
    public String toString() {
        return "" + (userDetails != null) ;
    }
}

Let me know, what is the problem you are facing with this approach.

like image 136
Avinash Avatar answered Nov 15 '22 00:11

Avinash