我正在使用Jersey javax Web服务实现Web服务。我从Postman休息客户端发送json对象到我的Web服务,但我无法在我的Web服务中处理该json。
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Person login(Person person) throws ClassNotFoundException, SQLException {
Person todo = new Person();
todo.setValid_user(true);
return todo;
}
我正在从邮递员休息客户端发送json到上面的函数,如下所示。
http://localhost:8080/RestWebService/rest/person/login
原始我正在写我的json输入
{"Person":[
"valid_user":"true"
"userId":"1",
"UserName":"parag",
"userPassword":"parag123",
"userLocation":"asd",
"userTime":"asda",
"message":"asdasd"
]}
但我得到500和415错误。 请帮帮我。
人员类:
package com.avilyne.rest.model;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class Person {
@JsonProperty("userId")
private String userId;
@JsonProperty("UserName")
private String UserName;
@JsonProperty("userPassword")
private String userPassword;
@JsonProperty("userLocation")
private String userLocation;
@JsonProperty("userTime")
private String userTime;
@JsonProperty("message")
private String message;
@JsonProperty("valid_user")
private String valid_user;
public String isValid_user() {
return valid_user;
}
public void setValid_user(String valid_user) {
this.valid_user = valid_user;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserLocation() {
return userLocation;
}
public void setUserLocation(String userLocation) {
this.userLocation = userLocation;
}
public String getUserTime() {
return userTime;
}
public void setUserTime(String userTime) {
this.userTime = userTime;
}
@Override
public String toString() {
return "Person [userId=" + userId + ", UserName=" + UserName + ", userPassword=" + userPassword
+ ", userLocation=" + userLocation + ", userTime=" + userTime + "]";
}
}
答案 0 :(得分:0)
使用杰克逊映射器,它会自动将json转换为你的“人物”。对象,反之亦然。
您只需更新您的“人物”即可。 bean并添加@JsonProperty和@JsonCreator注释(通过下面的链接),杰克逊将负责其余的工作。
https://github.com/FasterXML/jackson-annotations
有效的json应该是:
{
"valid_user":"true",
"userId":"1",
"userName":"parag",
"userPassword":"parag123",
"userLocation":"asd",
"userTime":"asda",
"message":"asdasd"
}
编辑:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
@JsonProperty("userId")
private String userId;
@JsonProperty("userName")
private String userName;
@JsonProperty("userPassword")
private String userPassword;
@JsonProperty("userLocation")
private String userLocation;
@JsonProperty("userTime")
private String userTime;
@JsonProperty("message")
private String message;
@JsonProperty("valid_user")
private boolean valid_user;
@JsonCreator
public Person(@JsonProperty("userId") String userId,
@JsonProperty("userName") String userName,
@JsonProperty("userPassword") String userPassword,
@JsonProperty("userLocation") String userLocation,
@JsonProperty("userTime") String userTime,
@JsonProperty("message") String message,
@JsonProperty("valid_user") boolean valid_user) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userLocation = userLocation;
this.userTime = userTime;
this.message = message;
this.valid_user = valid_user;
}
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public String getUserPassword() {
return userPassword;
}
public String getUserLocation() {
return userLocation;
}
public String getUserTime() {
return userTime;
}
public String getMessage() {
return message;
}
public boolean isValid_user() {
return valid_user;
}
@Override
public String toString() {
return "Person{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", userLocation='" + userLocation + '\'' +
", userTime='" + userTime + '\'' +
", message='" + message + '\'' +
", valid_user=" + valid_user +
'}';
}
}
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
@JsonProperty("userId")
private String userId;
@JsonProperty("userName")
private String userName;
@JsonProperty("userPassword")
private String userPassword;
@JsonProperty("userLocation")
private String userLocation;
@JsonProperty("userTime")
private String userTime;
@JsonProperty("message")
private String message;
@JsonProperty("valid_user")
private boolean valid_user;
@JsonCreator
public Person(@JsonProperty("userId") String userId,
@JsonProperty("userName") String userName,
@JsonProperty("userPassword") String userPassword,
@JsonProperty("userLocation") String userLocation,
@JsonProperty("userTime") String userTime,
@JsonProperty("message") String message,
@JsonProperty("valid_user") boolean valid_user) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userLocation = userLocation;
this.userTime = userTime;
this.message = message;
this.valid_user = valid_user;
}
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public String getUserPassword() {
return userPassword;
}
public String getUserLocation() {
return userLocation;
}
public String getUserTime() {
return userTime;
}
public String getMessage() {
return message;
}
public boolean isValid_user() {
return valid_user;
}
@Override
public String toString() {
return "Person{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", userLocation='" + userLocation + '\'' +
", userTime='" + userTime + '\'' +
", message='" + message + '\'' +
", valid_user=" + valid_user +
'}';
}
}
我更新了上面的json,你错过了','在valid_user之后我也改变了#34; UserName" to" userName" (如果你愿意,可以改变它)。
请测试一下,让我知道它是否有效。
答案 1 :(得分:0)
您的JSON 无效(使用JSONLint检查):
您服务中使用的有效JSON将是:
{
"valid_user": true,
"userId": "1",
"UserName": "parag",
"userPassword": "parag123",
"userLocation": "asd",
"userTime": "asda",
"message": "asdasd"
}
请注意,
和"valid_user":"true"
之后的{}
而不是[]
。
您不需要{ "Person": {...} }
。