Reputation: 8056
i am using keycloak and spring to get the user list in a rest service, however the rest return the html instead of json data.
here is the service.
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
KeycloakInstance keycloakInstance;
public List<UserRepresentation> loadUsers(String realm) {
return keycloakInstance.getInstance()
.realm(realm)
.users()
.list();
}
}
here is the controller.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.keycloak.representations.idm.UserRepresentation;
import java.util.List;
@RestController
@RequestMapping("/api/admin/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "", produces="application/json")
public List<UserRepresentation> loadUsers() {
String realm = "abc";
return userService.loadUsers(realm);
}
}
any idea how to fix this?
Upvotes: 3
Views: 4654
Reputation: 3610
You would need to add the path to @GetMapping
, this should work
@ResponseBody
@GetMapping(value = "/api/admin/users", produces="application/json")
public List<UserRepresentation> loadUsers() {
String realm = "abc";
return userService.loadUsers(realm);
}
or using this way
@GetMapping (value = "/api/admin/users", produces = MediaType.APPLICATION_JSON_VALUE)
Upvotes: 2
Reputation: 58774
As @ConstantinKonstantinidis commented, add to RequestMapping
produces JSON to apply to your method (and all methods):
@RequestMapping(value = "/api/admin/users", produces = MediaType.APPLICATION_JSON_VALUE
Supported at the type level as well as at the method level
Another option is to add the path to @GetMapping
,e.g.
@RequestMapping("/api/admin")
public class UserController {
@GetMapping(value = "/users", produces="application/json")
Upvotes: 1