SpringBootで値をセッション保持する(axiosからリクエストを投げる場合)
SpringBoot側で値を保持してほしいのになかなかできなくて困ってたが、時間が経ってゆっくり考えたら案外簡単にできた。
SpringBoot側
@RestController public class IndexController { @Autowired private AbcData abc; @PostMapping("/") @CrossOrigin(origins = { "http://localhost:3000/" }, allowCredentials = "true") public String postIndex() { abc.setA("test"); return "test"; } }
@Data @Component @Scope(value= "session", proxyMode = ScopedProxyMode.TARGET_CLASS) public class AbcData { private String a; }
axios側(React)
export const App = () => { return ( <div> <button onClick={async () => { const res = await axios.post<string>("http://localhost:8080/", null, {withCredentials: true}); console.log(res.data) }}>aaaaa</button> </div> ); }