I want to create a cookie :
@Path("/users")
@ApplicationScoped
public class UserResource {
private final UserService userService;
@Inject
public UserResource(UserService userService) {
this.userService = userService;
}
private String getDeviceName() {
try {
String deviceName = InetAddress.getLocalHost().getHostName();
return deviceName;
}
catch (UnknownHostException e) {
return "WEB";
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/tsite-user/{id}")
public Response retrieveBySiteUserId(Long id) {
NewCookie cookie = new NewCookie.Builder("deviceName").value(getDeviceName()).path("/").httpOnly(true).build();
try {
var client = userService.retrieveBySiteUserId(id);
return Response.ok(client).cookie(cookie).build();
} catch (DataNotFoundException e) {
Payload payload = new Payload("Error",e.getMessage());
return Response.status(Response.Status.NOT_FOUND).cookie(cookie).entity(payload).build();
}
}
....
}
In another resource I want to retrieve the cookie value :
@Path("/notes")
@ApplicationScoped
public class NoteRessource {
private final NoteService noteService;
...
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/pos/header")
public Response createPosNoteEntete(TPosNoteEntete note, @CookieParam("deviceName") String cookieValue) {
System.out.println("============== cookie = "+cookieValue);
try {
var newNote = noteService.createPosNoteEntete(note);
return Response.status(Response.Status.CREATED).entity(newNote).build();
} catch (DataConstraintException e) {
LOGGER.error("Error occured during note creation." + e.getStackTrace());
Payload payload = new Payload("Invalid payoad.",e.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(payload).build();
}
}
...
}
At runtime it shows ============== cookie = null
So what is wrong ?