Java Rest Service XML请求语法上不正确的格式

时间:2016-04-14 11:00:59

标签: java xml web-services rest post

我是网络服务新手。我正在构建一个Android应用程序,我正在尝试向Web服务发送POST请求,但我不确定格式是否正确。

这是REST中的POST方法:

@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,MediaType.TEXT_XML})
public void create(Users entity) {
    super.create(entity);
}

这些属性是与我们的表对应的类。我使用的是Oracle 11g数据库,Glassfish服务器4.1.1和Netbeans。我试图从我的Android应用程序调用PUT。有人可以建议一种方法吗?

public class Users implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "USER_ID")
private Short userId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "USERNAME")
private String username;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "PASSWORD")
private String password;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 40)
@Column(name = "EMAIL")
private String email;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "users")
private Profiles profiles;

我正在尝试使用正文调用POST方法:

`<users> 
<email> user@smth.com</email>
<userid>1</userid>
<password>pass</password>
<username>user</username>
</users>`

我该怎么称呼它?

2 个答案:

答案 0 :(得分:0)

您应该使用JAXB来序列化您的对象,或者创建您想要自己发布的xml的字符串表示形式。 要发送请求,请使用来自jaxrs https://docs.oracle.com/javaee/7/tutorial/jaxrs-client001.htm#BABBIHEJ的客户端api 或者如果您使用的是Spring RestTemplate

答案 1 :(得分:0)

您可以使用以下名称值对:

    List userdetails = new ArrayList();
userdetails.add(new BasicNameValuePair("email", "s@gmail.com"));
userdetails.add(new BasicNameValuePair("userid", "1"));
userdetails.add(new BasicNameValuePair("password", "5353$$"));
userdetails.add(new BasicNameValuePair("username", "shy"));

然后发布如下:

  // Making HTTP request
        try { 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("your url goes here");
httpPost.setEntity(new UrlEncodedFormEntity(userdetails));//params added here
httpPost.setHeader("Content-type","application/x-www-form-urlencoded");
httpPost.setHeader("Accept", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
Log.d("LINE",line);
sb.append(line + "\n");
} is.close();
json = sb.toString();
} catch (Exception e) { e.printStackTrace();
Log.e("Buffer Error", "Error converting result " + e.toString());
}
Log.d("RESULT",json);

您的网络服务中的Post方法如下所示:

    @POST
    @Consumes({MediaType.APPLICATION_XML,MediaType.TEXT_XML,MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    @Path("/insert")
    public Response insertRecord(Users users) {
    }