【问题标题】:Java Rest Service XML Request syntactically incorrect formatJava Rest Service XML 请求语法不正确的格式
【发布时间】:2016-04-14 11:00:58
【问题描述】:

我是网络服务的新手。我正在构建一个 android 应用程序,我正在尝试向网络服务发送一个 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>`

我应该怎么称呼它?

【问题讨论】:

    标签: java xml web-services rest post


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      列出 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"));

      然后发布如下:

      // 发出 HTTP 请求 试试{
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost("你的网址在这里");
      httpPost.setEntity(new UrlEncodedFormEntity(userdetails));//这里添加的参数
      httpPost.setHeader("内容类型","application/x-www-form-urlencoded");
      httpPost.setHeader("接受", "应用程序/json");
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      InputStream 是 = httpEntity.getContent();
      } 捕捉 (UnsupportedEncodingException e) {
      e.printStackTrace();
      } 捕捉 (ClientProtocolException e) {
      e.printStackTrace();
      } 捕捉 (IOException e) {
      e.printStackTrace();
      }
      试试{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
      StringBuilder sb = new StringBuilder();
      字符串 line = null;
      while ((line = reader.readLine()) != null) {
      Log.d("LINE",line);
      sb.append(line + "\n");
      } is.close();
      json = sb.toString();
      } 捕捉(异常 e){ e.printStackTrace();
      Log.e("缓冲区错误", "转换结果错误" + e.toString());
      }
      Log.d("结果",json);

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

      @邮政 @Consumes({MediaType.APPLICATION_XML,MediaType.TEXT_XML,MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}) @Path("/插入") 公共响应插入记录(用户用户){ }

      【讨论】:

      • 此处的示例 Web 服务实现:link
      • 我尝试在 POST 方法 MediaType.APPLICATION_FORM_URLENCODED_TYPE 中添加 Consumes 括号,但它不允许我说它无法转换为 String。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2021-12-11
      • 1970-01-01
      • 2016-07-24
      • 2017-04-13
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多