0 java rest post json dropwizard
public class UserProfileData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "lastName", nullable = true)
private String lastName;
@Column(name = "country", nullable = true)
private String country;
public UserProfileData() {
}
public UserProfileData(String firstName, String lastName, String country) {
this.firstName = firstName;
this.lastName = lastName;
this.country = country;
}
public long getId() {
return id;
}
public void setObjectId(long id) {
this.id = id;
}
@JsonProperty
public String getFirstName() {
return firstName;
}
@JsonProperty
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@JsonProperty
public String getLastName() {
return lastName;
}
@JsonProperty
public void setLastName(String lastName) {
this.lastName = lastName;
}
@JsonProperty
public String getCountry() {
return country;
}
@JsonProperty
public void setCountry(String country) {
this.country = country;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UserProfileData)) return false;
final UserProfileData that = (UserProfileData) o;
return Objects.equals(this.id, that.id) &&
Objects.equals(this.firstName, that.firstName) &&
Objects.equals(this.lastName, that.lastName) &&
Objects.equals(this.country, that.country);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, country);
}
}
========================================================================
DAO Method
public UserProfileData create(UserProfileData userProfileData) {
LOGGER.info("UserProfileData: Persisting New User");
return persist(userProfileData);
}
=======================================================================
Resource Call
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserProfileDataResource {
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@UnitOfWork
public UserProfileData createUser(UserProfileData userProfileData) {
//ResourceHelper.checkRequiredParams(requesterId);
LOGGER.info("User created");
return userProfileDataDAO.create(userProfileData);
}
======================================================================
Run Code Online (Sandbox Code Playgroud)
当我调用资源用于创建新的用户数据时,它给了我错误
curl -H "Content-Type: application/json" -X POST -d "{"firstName":"Sachin","lastName":"Tendulkar","country":"India"}" http://localhost: 8080/用户/创建
错误:{"code":400,"message":"无法处理 JSON","details":null}
尝试了几乎所有内容,但无法弄清楚为什么会出现此错误以及如何解决?
要获得有关 400 错误的有用消息,请在球衣上注册:
environment.jersey().register(new JsonProcessingExceptionMapper(true));
Run Code Online (Sandbox Code Playgroud)
它将提供有关 400 响应的更详细消息。