这可能只是一个愚蠢的错误,但它让我陷入了很长一段时间,而且这里没有其他类似问题的答案对我有用。
我有一个Java的REST Web服务(在Eclipse中创建,托管在Tomcat中),它应该接受JSON中的POST登录。
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
Dao dao = new Dao();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
return dao.getAllUsers();
}
@POST
@Consumes("application/json")
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
public User login(final LoginBean params){
return new User();
/*User u = new User();
u.setEmail(params.email);
u.setPassword(params.password);
return dao.login(u);*/
}
}
正如你所看到的,方法体是没用的,因为我试图找出它为什么不接受我的电话,所以我确定问题不在体内。
我正在使用Postman来称呼它。
我得到了这个结果(措辞可能与标准略有不同,因为由于某些原因我的是法语(可能是某种程度上Windows的错误),而且我已经大致翻译了重要的部分):
<!DOCTYPE html>
<html>
<head>
<title>Apache Tomcat/9.0.0.M17 - Rapport d''erreur</title>
<style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style>
</head>
<body>
<h1>Etat HTTP 415 - Unsupported Media Type</h1>
<div class="line"></div>
<p>
<b>type</b> Status Report
</p>
<p>
<b>message</b>
<u>Unsupported Media Type</u>
</p>
<p>
<b>description</b>
<u>The server has denied this request because the request entity is in a format that is not supported by the queried resource with the specified method.</u>
</p>
<hr class="line">
<h3>Apache Tomcat/9.0.0.M17</h3>
</body>
</html>
以下是两个重要的课程:
@XmlRootElement
public class LoginBean {
@XmlElement public String email;
@XmlElement public String password;
}
@XmlRootElement(name = "user")
public class User implements Serializable {
public enum accountType {
Admin,
User
}
private static final long serialVersionUID = 1L;
private int _id;
private String _firstName;
private String _lastName;
private String _email;
private String _password;
private Date _registerDate;
private Date _lastOnlineDate;
private Time _lastOnlineTime;
private String _lastOnlineIP;
private accountType _type;
private String _country;
private String _province;
public User(int id){
this._id = id;
}
public User() {
this._id = -1;
}
public int getId() {
return _id;
}
@XmlElement
public void setId(int id) {
this._id = id;
}
public String getFirstName() {
return _firstName;
}
public void setFirstName(String firstName) {
this._firstName = firstName;
}
(lots of skipped content here)
public boolean isValid() {
if (isNullOrWhitespace(_email))
return false;
if (isNullOrWhitespace(_password))
return false;
if (_id < 1)
return false;
return true;
}
}