2013-11-28 81 views
0

我正在使用Spring 4.0 REST API創建Web服務Api,我有以下Android HttpClient代碼,但我得到服務器的404錯誤,我需要最佳的Spring控制器代碼,這將與我的以下Android HttpClientStringEntity的Spring REST控制器

其中有一個方法如下
void call() { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://192.168.0.15:8080/locate/register"); 
     post.setHeader("Content-type", "application/json"); 
     post.setHeader("Accept", "application/json"); 
     JSONObject obj = new JSONObject(); 
     obj.put("userId", "1111"); 
     obj.put("mobileNumber", "1111111"); 
      obj.put("userName", "abc"); 
      obj.put("password", "abc"); 
      obj.put("email", "[email protected]"); 
      obj.put("meiNumber", "876535354"); 
      post.setEntity(new StringEntity(obj.toString(), "UTF-8")); 
     HttpResponse response = client.execute(post); 
    } 

目前我已經寫了春天控制器,我想回到同一對象或任何在JSON而已,也解釋了爲什麼下面的方法是不符合我的Android客戶端代碼工作..

@RequestMapping(method = RequestMethod.POST, value = "/register", headers = "Accept=application/json") 
    public @ResponseBody UserRegistration registerUser(@RequestBody UserRegistration user) { 
       // my business logic 
       return user; 
    } 

我是pastin摹放在userRegistration模型這裏

public class UserRegistration implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private long userId; 
    private String mobileNumber; 
    private String userName; 
    private String password; 
    private String email; 
    private String imeiNumber; 

    private Date registrationDate; 


    public long getUserId() { 
     return userId; 
    } 

    public void setUserId(long userId) { 
     this.userId = userId; 
    } 


    public String getMobileNumber() { 
     return mobileNumber; 
    } 

    public void setMobileNumber(String mobileNumber) { 
     this.mobileNumber = mobileNumber; 
    } 


    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 


    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 


    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 


    public String getImeiNumber() { 
     return imeiNumber; 
    } 

    public void setImeiNumber(String imeiNumber) { 
     this.imeiNumber = imeiNumber; 
    } 

    public Date getRegistrationDate() { 
     return registrationDate; 
    } 

    public void setRegistrationDate(Date registrationDate) { 
     this.registrationDate = registrationDate; 
    } 
} 

請幫我解決這個..

+0

我不知道你可以從android調用「http:// localhost:8080」...嘗試在「http://jelastic.com/」上放置web服務(他們有免費的計劃)。請添加logcat。 –

+0

@Haris是在Android客戶端我使用我的網絡我的IP,那麼我也會得到相同的錯誤 – Ani

回答

0

當您使用在Android上StringEntity,它被附加到名稱爲「數據」的參數。所以,你需要的是這樣的:

@RequestMapping(method = RequestMethod.POST, value = "/register", headers = "Accept=application/json") 
    public @ResponseBody UserRegistration registerUser(@RequestParam(value = "data") UserRegistration user) { 
       // my business logic 
       return user; 
    } 

您可能需要添加一個轉換器,像這樣:

public class StringToUserRegistrationConverter implements Converter<String, UserRegistration> { 
    Logger log = Logger.getLogger("StringToUserRegistrationConverter"); 

    @Override 
    public UserRegistration convert(String source) { 
     ObjectMapper objectMapper = new ObjectMapper(); 
     try { 
      return objectMapper.readValue(source, UserRegistration.class); 
     } catch (IOException e) { 
      log.log(Level.SEVERE, "Couldn't convert", e); 
     } 
     return null; 
    } 
} 

並註冊該轉換器:

<bean id="conversionService" 
     class="org.springframework.context.support.ConversionServiceFactoryBean"> 
    <property name="converters"> 
     <set> 
      <bean class="com.example.spring.converter.StringToUserRegistrationConverter"/> 
     </set> 
    </property> 
    </bean> 
相關問題