title: java.io.IOException With Restful Web Service
body: <p>I have a weird problem with my project. When i run <code>main method</code> in <code>Client</code> to create a <code>User</code> after send to <code>Server</code> then occur fllowing error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.io.IOException: Server returned HTTP
    response code: 400 for URL: <a href="http://localhost:8080/rest/user/create" rel="nofollow">http://localhost:8080/rest/user/create</a>    at
    sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown
    Source)   at
    sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
    Source)   at
    edu.java.spring.service.client.RestClientTest.createUser(RestClientTest.java:72)
      at
    edu.java.spring.service.client.RestClientTest.main(RestClientTest.java:30)</p>
  </blockquote>
  
  <p>Here file <code>RestClientTest.java</code></p>
  
  <pre><code>public class RestClientTest {
      public static void main(String[] args) throws IOException{
  //      System.out.println("Rest Response" + loadUser("quypham"));
          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);
          createUser(user);
          System.out.println("Rest Response" + loadUser("datpham"));
      }
      public static String createUser(User user) throws IOException{
  
          URL url = new URL("http://localhost:8080/rest/user/create");
          HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
          httpConnection.setRequestMethod("POST");
          httpConnection.addRequestProperty("Content-Type","application/json");
          httpConnection.addRequestProperty("Accept","application/json");
          httpConnection.setDoOutput(true);
  
          DataOutputStream wr = new DataOutputStream(httpConnection.getOutputStream());
  
          ObjectMapper mapper = new ObjectMapper();
          StringWriter writer = new StringWriter();
          mapper.writeValue(writer,user);
          wr.writeBytes(writer.getBuffer().toString());
          wr.flush();
          wr.close();
          return readInputStream(httpConnection.getInputStream());
  
  
      }
      public static String readInputStream(InputStream stream) throws IOException{
          StringWriter writer = new StringWriter();
          try {
              int read;
              byte[] bytes = new byte[4*1024];
              while ((read = stream.read(bytes))!=-1) {
                  writer.write(new String(bytes,0,read));
  
  
              }
          } finally {
              // TODO: handle finally clause
              stream.close();
              writer.close();
          }
          return writer.getBuffer().toString();
  
      }
  </code></pre>
  
  <p>Here file <code>UserRestServiceController.java</code></p>
  
  <pre><code>@Controller
  public class UserRestServiceController {
      @Autowired
      public  UserDao userDao;
      @RequestMapping(value = "/rest/user/create",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
      @ResponseStatus(HttpStatus.CREATED)
      public void addUser(@RequestBody User user){
          userDao.save(user);
      }
  </code></pre>