StringEntity的Spring REST控制器

时间:2013-11-28 18:23:55

标签: java android spring web-services spring-mvc

我正在使用Spring 4.0 REST Api创建Web服务,我正在使用Android HttpClient代码但是我从服务器收到404错误,我需要最好的Spring控制器代码,它将与我的以下Android HttpClient一起使用

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", "abc@a.com");
            obj.put("meiNumber", "876535354");
            post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
        HttpResponse response = client.execute(post);
    }

目前我已经编写了一个Spring控制器,它有一个方法如下,我想返回同一个对象或只有JSON的东西,也解释了为什么以下方法不能用我的android客户端代码..

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

我在这里粘贴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;
    }
}

请帮我解决这个问题..

1 个答案:

答案 0 :(得分:0)

在Android中使用StringEntity时,会将其附加到名称为" data"的参数中。所以你需要的是:

@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>