I'm using ResTemplate to post User object in CLient. But When i use method postForObject then occur Unresolved compilation problem The method postForObject(URI, Object, Class<T>) in the type RestTemplate is not applicable for the arguments (URL, User, Class<User>)
. I really don't understand ???
Here file RestClientTest
.java`
public class RestClientTest {
public static void main(String[] args) throws IOException{
// System.out.println("Rest Response" + loadUser("quypham"));
// URL url = new URL("http://localhost:8080/rest/user/create");
// rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
// rt.getMessageConverters().add(new StringHttpMessageConverter());
// Map<String,String> vars = new HashMap<String,String>();
RestTemplate rt = new RestTemplate();
User user = new User();
user.setUserName("datpham");
user.setPassWord("12345");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,1960);
user.setBirthDay(calendar.getTime());
user.setAge(12);
String uri = new String("http://localhost:8080/rest/user/create");
User returns = rt.postForObject(uri, user,User.class);
// createUser(user);
System.out.println("Rest Response" + loadUser("datpham"));
}
Here file UserRestServiceController
@Controller
public class UserRestServiceController {
@Autowired
public UserDao userDao;
@RequestMapping(value = "/rest/user/create",method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void addUser(@RequestBody User user){
userDao.save(user);
}
I have edited String uri
but encounter new following error:
Mar 29, 2016 1:57:43 PM org.springframework.web.client.RestTemplate handleResponseError WARNING: POST request for "http://localhost:8080/rest/user/create" resulted in 400 (Bad Request); invoking error handler Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:588) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:546) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502) at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:330) at edu.java.spring.service.client.RestClientTest.main(RestClientTest.java:45)
Here User.java
@Entity
@Table(name = "brotheruser",uniqueConstraints={@UniqueConstraint(columnNames="username")})
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
// @Enumerated(EnumType.STRING)
// @Column(name = "gender", nullable = false)
//
// public Gender getGender() {
// return gender;
// }
// public void setGender(Gender gender) {
// this.gender = gender;
// }
@Id
@Column(name = "username", unique = true, nullable = false)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "password", nullable = false)
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@JsonSerialize(using = DateSerializer.class)
// @JsonDeserialize(using = DateDeserializer.class)
@Column(name = "birthday", nullable = false)
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Column(name="age", nullable = false)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private String userName;
private String passWord;
private Date birthDay;
private Integer age;
// private Gender gender;
}