REST API Response.ok(entity).build() 不显示文本
REST API Response.ok(entity).build() doesn't show text
我正在为学校练习创建一个简单的 Restful API,但我 运行 遇到了一个问题。当我尝试显示实体列表或网页上的单个实体时,它不显示任何文本。它似乎确实找到了正确的实体(当我过滤时,它显示了正确数量的实体),它只是不显示文本。也许它不是 运行 ToString?但我没有发现我的 ToString 方法有任何问题。
这是一个例子:
我的 UserResources 中 return 用户的功能:
@GET //GET at http://localhost:XXXX/users/1
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUserPath(@PathParam("id") int Id) {
User user = fakeDatabase.getUser(Id);
if (user == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Please provide a valid user ID.").build();
} else {
return Response.ok(user).build();
}
}
在 FakeDatabase 中获取用户:
public User getUser(int userId) {
for (User user : userList) {
if (user.GetUserId() == userId) {
return user;
}
}
return null;
}
用户 ToString:
@Override
public String toString() {
String WishlistItems = "";
for(String item : Wishlist){
WishlistItems = WishlistItems + item + ", ";
}
return "User (" + UserId + ") {" + "\n" +
"Email Address = " + EmailAddress + "\n" +
"UserName = " + UserName + "\n" +
Platform.toString() + " ID = " + PlatformID + "\n" +
"Wishlist :" + WishlistItems + "\n" +
"}" + "\n";
}
我在网站上得到的结果:
有人知道这个问题的解决方案吗?
编辑:完整用户class(我还更新了 toString 方法):
@XmlRootElement
public class User {
private int UserId;
private String UserName;
private String EmailAddress;
private int PasswordHash;
private Platform Platform;
private String PlatformID;
private ArrayList<String> Wishlist = new ArrayList<String>();
public User(int userId, String Password, String emailAddress, String userName, Platform platform, String platformID){
UserId = userId;
EmailAddress = emailAddress;
UserName = userName;
Platform = platform;
PlatformID = platformID;
hashPassword(Password);
}
public User(){
}
public int GetUserId(){
return UserId;
}
public String GetUserName(){
return UserName;
}
public void SetUserName(String UserName){
this.UserName = UserName;
}
public String GetEmailAddress(){
return EmailAddress;
}
public void SetEmailAddress(String EmailAddress){
this.EmailAddress = EmailAddress;
}
public Platform GetPlatform(){
return Platform;
}
public void SetPlatform(Platform Platform){
this.Platform = Platform;
}
public String GetPlatformID(){
return PlatformID;
}
public void SetPlatformID(String PlatformID){
this.PlatformID = PlatformID;
}
public void AddToWishlist(String item){
Wishlist.add(item);
}
public ArrayList<String> GetWishlist(){
return Wishlist;
}
public void SetWishlist(ArrayList<String> Wishlist){
this.Wishlist = Wishlist;
}
public void hashPassword(String Password) {
PasswordHash = Objects.hash(Password);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return UserId == user.GetUserId();
}
@Override
public String toString() {
String WishlistItems = "";
for(String item : Wishlist){
WishlistItems = WishlistItems + item + ", ";
}
return "User (" + UserId + ") {" + "\n" +
"Email Address = " + EmailAddress + "\n" +
"UserName = " + UserName + "\n" +
Platform.toString() + " ID = " + PlatformID + "\n" +
"Wishlist :" + WishlistItems + "\n" +
"}" + "\n";
}
}
您用 Spring 引导标记了您的问题,但 Spring 引导代码看起来确实不同。用类似的东西试试:
@GetMapping("/users/{id}")
public ResponseEntity<User> getUserPath(@PathVariable("id") int Id) {
User user = fakeDatabase.getUser(Id);
if (user == null) {
return ResponseEntity.badRequest().body("Please provide a valid user ID.");
} else {
return ResponseEntity.ok(user);
}
}
响应实体未通过 toString()
方法序列化。如果你想 return 一个字符串,那么你必须通过自己调用 user.toString()
来设置一个字符串实体。
如果将对象设置为实体,它将被视为 POJO。这意味着您必须为属性定义适当的 setXXX/getXXX 方法。您的 User
class 使用了错误的大小写。
查看 Set a Response Body in JAX-RS 了解更多详细信息(尤其是第 3.4 节。JSON 使用 POJO 的响应可能符合您的兴趣)
这是一个目的:
@RestController
@RequestMapping("/user")
public class UserController {
@GET //GET at http://localhost:XXXX/users/1
@Path("/{id}")
@Produces(MediaType.APPLICATION_XML)
public Response<User> getUserPath(@PathParam("id") int Id) {
return fakeDatabase.getUser(Id).ifPresentOrElse(u -> Response.ok(u), return Response.empty());
}
}
public class FakeBase {
public Optional<User> getUser(int userId) {
return userList.stream().filter(u -> u.hasId(userId)).findFirst();
}
}
public class User () {
public boolean hasId(int id) {
return this.id == id;
}
}
User.toString()
没用
我正在为学校练习创建一个简单的 Restful API,但我 运行 遇到了一个问题。当我尝试显示实体列表或网页上的单个实体时,它不显示任何文本。它似乎确实找到了正确的实体(当我过滤时,它显示了正确数量的实体),它只是不显示文本。也许它不是 运行 ToString?但我没有发现我的 ToString 方法有任何问题。
这是一个例子:
我的 UserResources 中 return 用户的功能:
@GET //GET at http://localhost:XXXX/users/1
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUserPath(@PathParam("id") int Id) {
User user = fakeDatabase.getUser(Id);
if (user == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Please provide a valid user ID.").build();
} else {
return Response.ok(user).build();
}
}
在 FakeDatabase 中获取用户:
public User getUser(int userId) {
for (User user : userList) {
if (user.GetUserId() == userId) {
return user;
}
}
return null;
}
用户 ToString:
@Override
public String toString() {
String WishlistItems = "";
for(String item : Wishlist){
WishlistItems = WishlistItems + item + ", ";
}
return "User (" + UserId + ") {" + "\n" +
"Email Address = " + EmailAddress + "\n" +
"UserName = " + UserName + "\n" +
Platform.toString() + " ID = " + PlatformID + "\n" +
"Wishlist :" + WishlistItems + "\n" +
"}" + "\n";
}
我在网站上得到的结果:
有人知道这个问题的解决方案吗?
编辑:完整用户class(我还更新了 toString 方法):
@XmlRootElement
public class User {
private int UserId;
private String UserName;
private String EmailAddress;
private int PasswordHash;
private Platform Platform;
private String PlatformID;
private ArrayList<String> Wishlist = new ArrayList<String>();
public User(int userId, String Password, String emailAddress, String userName, Platform platform, String platformID){
UserId = userId;
EmailAddress = emailAddress;
UserName = userName;
Platform = platform;
PlatformID = platformID;
hashPassword(Password);
}
public User(){
}
public int GetUserId(){
return UserId;
}
public String GetUserName(){
return UserName;
}
public void SetUserName(String UserName){
this.UserName = UserName;
}
public String GetEmailAddress(){
return EmailAddress;
}
public void SetEmailAddress(String EmailAddress){
this.EmailAddress = EmailAddress;
}
public Platform GetPlatform(){
return Platform;
}
public void SetPlatform(Platform Platform){
this.Platform = Platform;
}
public String GetPlatformID(){
return PlatformID;
}
public void SetPlatformID(String PlatformID){
this.PlatformID = PlatformID;
}
public void AddToWishlist(String item){
Wishlist.add(item);
}
public ArrayList<String> GetWishlist(){
return Wishlist;
}
public void SetWishlist(ArrayList<String> Wishlist){
this.Wishlist = Wishlist;
}
public void hashPassword(String Password) {
PasswordHash = Objects.hash(Password);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return UserId == user.GetUserId();
}
@Override
public String toString() {
String WishlistItems = "";
for(String item : Wishlist){
WishlistItems = WishlistItems + item + ", ";
}
return "User (" + UserId + ") {" + "\n" +
"Email Address = " + EmailAddress + "\n" +
"UserName = " + UserName + "\n" +
Platform.toString() + " ID = " + PlatformID + "\n" +
"Wishlist :" + WishlistItems + "\n" +
"}" + "\n";
}
}
您用 Spring 引导标记了您的问题,但 Spring 引导代码看起来确实不同。用类似的东西试试:
@GetMapping("/users/{id}")
public ResponseEntity<User> getUserPath(@PathVariable("id") int Id) {
User user = fakeDatabase.getUser(Id);
if (user == null) {
return ResponseEntity.badRequest().body("Please provide a valid user ID.");
} else {
return ResponseEntity.ok(user);
}
}
响应实体未通过 toString()
方法序列化。如果你想 return 一个字符串,那么你必须通过自己调用 user.toString()
来设置一个字符串实体。
如果将对象设置为实体,它将被视为 POJO。这意味着您必须为属性定义适当的 setXXX/getXXX 方法。您的 User
class 使用了错误的大小写。
查看 Set a Response Body in JAX-RS 了解更多详细信息(尤其是第 3.4 节。JSON 使用 POJO 的响应可能符合您的兴趣)
这是一个目的:
@RestController
@RequestMapping("/user")
public class UserController {
@GET //GET at http://localhost:XXXX/users/1
@Path("/{id}")
@Produces(MediaType.APPLICATION_XML)
public Response<User> getUserPath(@PathParam("id") int Id) {
return fakeDatabase.getUser(Id).ifPresentOrElse(u -> Response.ok(u), return Response.empty());
}
}
public class FakeBase {
public Optional<User> getUser(int userId) {
return userList.stream().filter(u -> u.hasId(userId)).findFirst();
}
}
public class User () {
public boolean hasId(int id) {
return this.id == id;
}
}
User.toString()
没用