sorry for asking again but i cant find the solution for my problem - neither here, nor anywhere else. I have a RESTful server using jersey who should consume a JSON via ajax from the client....but it returns a null value. Why??
很抱歉再次询问,但我无法找到问题的解决方案 - 无论是在这里,还是在其他任何地方。我有一个使用泽西的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);
}
the object:
@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;
}
}
and the AJAX call:
和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);
}
});
};
i don't get it...coordinate and RouteId are "null" when i try to extract them that way (via @queryParam)
我不明白...当我尝试以那种方式提取它时,坐标和RouteId是“空”(通过@queryParam)
1 个解决方案
#1
1
As far as I understand, you can only use @QueryParam
for GET requests.
据我了解,您只能将@QueryParam用于GET请求。
For POSTs you can put the object you want to populate as the method parameter and Jersey will instantiate it automatically for you.
对于POST,您可以将要填充的对象作为方法参数,Jersey将自动为您实例化。
I think you just have to ensure that the object to instantiate has the appropriate getters and setters.
我认为你必须确保实例化的对象具有适当的getter和setter。
A GET example:
一个GET示例:
@GET
@Path("logout")
@Produces(MediaType.APPLICATION_JSON)
public Response logout(@NotNull @QueryParam("user_id") int userId,
@NotNull @QueryParam("token") String token) {
A POST example:
一个POST示例:
@POST
@Path("register")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response register(RegisterUserParams registerUserParams) {
#1
1
As far as I understand, you can only use @QueryParam
for GET requests.
据我了解,您只能将@QueryParam用于GET请求。
For POSTs you can put the object you want to populate as the method parameter and Jersey will instantiate it automatically for you.
对于POST,您可以将要填充的对象作为方法参数,Jersey将自动为您实例化。
I think you just have to ensure that the object to instantiate has the appropriate getters and setters.
我认为你必须确保实例化的对象具有适当的getter和setter。
A GET example:
一个GET示例:
@GET
@Path("logout")
@Produces(MediaType.APPLICATION_JSON)
public Response logout(@NotNull @QueryParam("user_id") int userId,
@NotNull @QueryParam("token") String token) {
A POST example:
一个POST示例:
@POST
@Path("register")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response register(RegisterUserParams registerUserParams) {