0

我正在开发一个项目,并且在如何使用 php curl 将数据发布到 java restful webservice 方面遇到了一些困难。

SQL:

DROP DATABASE IF EXISTS POST_TESTE;
CREATE DATABASE POST_TESTE;
USE POST_TESTE;

CREATE TABLE GRAU  (
ID_GRAU             TINYINT         UNSIGNED        NOT NULL,
DESCRICAO_GRAU      VARCHAR(20)     NOT NULL
);

ALTER TABLE GRAU ADD CONSTRAINT PK_GRAU PRIMARY KEY (ID_GRAU);
ALTER TABLE GRAU CHANGE ID_GRAU ID_GRAU TINYINT UNSIGNED NOT NULL AUTO_INCREMENT;

INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('CET');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Licenciatura');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Pós-Licenciatura');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Pós-Graduação');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Mestrado');
INSERT INTO GRAU (DESCRICAO_GRAU) VALUES ('Doutoramento');

在 Netbeans 中,刚刚创建了一个名为“Teste”的 web 应用程序,然后从数据库中创建了 restful webservices,我在其中选择了上面创建的表。创建了 2 个包,第一个(实体)有一个与表 vars 相关的类,第二个(服务)与提供的服务相关。

所以我们的消费服务 url 是 http://localhost:8080/Teste/resources/entity.grau

Grau.java:

@Entity
@Table(name = "grau")
@XmlRootElement
@XmlType(propOrder={"idGrau", "descricaoGrau"})
@NamedQueries({
    @NamedQuery(name = "Grau.findAll", query = "SELECT g FROM Grau g"),
    @NamedQuery(name = "Grau.findByIdGrau", query = "SELECT g FROM Grau g WHERE g.idGrau = :idGrau"),
    @NamedQuery(name = "Grau.findByDescricaoGrau", query = "SELECT g FROM Grau g WHERE g.descricaoGrau = :descricaoGrau")})
public class Grau implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_GRAU")
    private Short idGrau;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "DESCRICAO_GRAU")
    private String descricaoGrau;

GrauFacadeREST.java :

@Stateless
@Path("entity.grau")
public class GrauFacadeREST extends AbstractFacade<Grau> {

@PersistenceContext(unitName = "TestePU")
private EntityManager em;

public GrauFacadeREST() {
    super(Grau.class);
}

@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Grau entity) {
    super.create(entity);
}

@PUT
@Override
@Consumes({"application/xml", "application/json"})
public void edit(Grau entity) {
    super.edit(entity);
}

还有更多的服务被开发出来,比如 GET、DELETE 服务,但我只想了解这两个。

我已经开发了一个带有一个输入文本的基本 html,它将数据发布到 .php,这个 .php 的目标是捕获信息并将其发布到例如create方法,以向表中添加新的 grau。

teste.html:

<html><body>
<form action="teste.php" method="post">
        <input type="text" name="text1" /></br>
    <input type="submit" value="Submeter" />
</form>
</body></html>

teste.php:

<?php

$var=$_POST['text1'];

$xml = new SimpleXMLElement('<graus/>');
$teste = $xml->addChild('grau');
$teste -> addChild('idGrau', '7');
$teste -> addChild('descricaoGrau', $var);
$teste1 = $xml->addChild('grau');
$teste1 -> addChild('idGrau', '8');
$teste1 -> addChild('descricaoGrau', $var);

//$xml="<graus><grau><idgrau>7</idgrau><descricaograu>asdf</descricaograu></grau><grau><idgrau>8</idgrau><descricaograu>asdf</descricaograu></grau></graus>";

/* $xml = array(
        "idgrau" => '7',
        "descricaograu" => $var,
);
*/

print($xml->asXML());

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/Teste/resources/entity.grau/');
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data=curl_exec($ch); 

if (curl_errno($ch)){
    echo curl_error($ch);
    exit;
}   
var_dump($data);
curl_close($ch);

?>

我已经以不同的方式尝试了这个,改变发送后域,在后域中注入数组等等,仍然说同样的错误:

HTTP 状态 400 - 错误请求 客户端发送的请求在语法上不正确(错误请求)。

当用户使用 findAll() 方法时,这是一个通过 .php 开发的响应式 xml。

<graus>
    <grau>
        <idGrau>1</idGrau>
        <descricaoGrau>CET</descricaoGrau>
    </grau>
    <grau>
        <idGrau>2</idGrau>
        <descricaoGrau>Licenciatura</descricaoGrau>
    </grau>
    <grau>
        <idGrau>3</idGrau>
        <descricaoGrau>Pós-Licenciatura</descricaoGrau>
    </grau>
    <grau>
        <idGrau>4</idGrau>
        <descricaoGrau>Pós-Graduação</descricaoGrau>
    </grau>
    <grau>
        <idGrau>5</idGrau>
        <descricaoGrau>Mestrado</descricaoGrau>
    </grau>
    <grau>
        <idGrau>6</idGrau>
        <descricaoGrau>Doutoramento</descricaoGrau>
    </grau>
</graus>

如果有人可以帮助我,我将不胜感激,因为我已经浪费了很多时间尝试这个并且仍然不明白如何去做。

4

0 回答 0