When jsp sends data value, java receives it.
$.ajax({
url: "login",
type: 'post',
data: JSON.stringify(<%=json%>),
contentType: 'application/json',
dataType: 'json'
});
So it works fine in Java. Now I want to send this to TypeScript.
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody JwtAuthenticationRequest authRequest) {
authRequest.setPassword(authRequest.getPassword());
authRequest.setUsername(authRequest.getLoginid());
try {
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
authRequest.getUsername(),
authRequest.getPassword()
);
authToken.setDetails(authRequest.getSecurebox());
Authentication authentication = authenticationManager.authenticate(authToken);
AuthUser authUser = (AuthUser) authentication.getPrincipal();
String accessToken = jwtTokenProvider.createToken(authUser);
String deviceId = authRequest.getDeviceId();
if (deviceId == null) {
deviceId = refreshTokenService.createDeviceId();
System.out.println("deviceId"+deviceId);
}
String refreshToken = refreshTokenService.createRefreshToken(authUser.getUsername(), deviceId);
Map<Object, Object> model = new HashMap<>();
model.put("user", authUser);
model.put("device_id", deviceId);
model.put("token", accessToken);
model.put("refresh_token", refreshToken);
System.out.println(">>>>>model : "+model);
return ok(model);
} catch (AuthenticationException e) {
throw new BadCredentialsException("ID or Password is incorrect.");
}
}
In the past, when TypeScript made a request to axios and sent data, it was received and sent in Java.
But now things have changed and I want to send data from jsp to java and then send it back to typescript.
function adminlogin(username: String, password: String): Promise<any> {
const deviceId = localStorage.getItem('deviceId');
return axios.post<any>('/login', {
username: username,
password: password,
deviceId: deviceId
}).then((response) => {
const user: AuthUser = response.data['user'] as AuthUser;
const deviceId: string = response.data['device_id'] as string;
const token: string = response.data['token'] as string;
const refreshToken: string = response.data['refresh_token'] as string;
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
localStorage.setItem('deviceId', deviceId);
localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
authenticationService.currentUser = user;
return user;
});
}
I don’t know how to give a return value in Java.
And I don’t know how to receive the return value from TypeScript.
I don’t know what to do in this case.
seullbee Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.