抱歉再次询问,但我无法找到解决问题的方法 - 无论是在这里,还是在其他任何地方。我有一个使用jersey的RESTful服务器,它应该从客户端通过ajax使用JSON ....但它返回一个空值。为什么?
REST:
@POST
@Path("/addPoint")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public WayPoint addPoint(@QueryParam("coordinate")String coordinate, @QueryParam("RouteId")Long RouteId) {
WayPoint waypoint = new WayPoint();
waypoint.setCoordinate(coordinate);
waypoint.setRouteId(RouteId);
System.out.println(waypoint);
return getEntityManager().merge(waypoint);
}
对象:
@Entity
@XmlRootElement
public class WayPoint {
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String coordinate;
private Long RouteId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCoordinate() {
return coordinate;
}
public void setCoordinate(String coordinate) {
this.coordinate = coordinate;
}
public Long getRouteId() {
return RouteId;
}
public void setRouteId(Long routeId) {
this.RouteId = routeId;
}
}
和AJAX电话:
this.AddPoint = function(coords, routeid){
var test = JSON.stringify({ coordinate: coords, RouteId: routeid });
console.log(test);
$.ajax({
url: thePath,
type: "POST",
data: JSON.stringify({ coordinate: coords, RouteId: routeid }),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
alert("new point added!");
console.log(response);
},
error:function(res){
alert("Cannot save point! " + res.statusText);
}
});
};
我不明白...当我尝试以这种方式提取它们时,坐标和RouteId为“null”(通过@queryParam)
答案 0 :(得分:1)
据我了解,您只能将@QueryParam
用于GET请求。
对于POST,您可以将要填充的对象作为方法参数,Jersey会自动为您实例化。
我认为你必须确保实例化的对象具有适当的getter和setter。
GET示例:
@GET
@Path("logout")
@Produces(MediaType.APPLICATION_JSON)
public Response logout(@NotNull @QueryParam("user_id") int userId,
@NotNull @QueryParam("token") String token) {
一个POST示例:
@POST
@Path("register")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response register(RegisterUserParams registerUserParams) {