有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有POST表单的Java/JERSY REST客户端异常

Restfull客户端不工作

我在服务器上的Restfull服务运行良好。我用一个表单调用服务,得到一个JSON格式的漂亮响应。但是,如果我为服务编写了一个带有main方法的客户机,那么该客户机将无法工作

以下客户端代码异常

List<Materialgrundlagen> matList = jsonAnswer.readEntity(new   GenericType<List<Materialgrundlagen>>() {});

例外情况

未找到媒体类型=应用程序/json,类型=接口java的MessageBodyReader。util。列表,genericType=java。util。名单

我使用以下组件

  1. 玻璃鱼4.1.1
  2. 来自EclipseLink的最新OSGI捆绑包
  3. Jersy whis包含在模块下的Glassfish中

这是在浏览器上工作的表单

<!Doctype html>
<html>
<head>
<title>Read VAN</title>
<meta charset="UTF-8"></meta>


</head>
<body>
 <form action="http://hilweb05:8080/RestServiceExample5/matgi/SomeService/" method="POST">
<label for="van">Systembezeichnung:</label>
<input name="van" />
<br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>

这是数据类(JPA实体和XxmlRootElement)

package de.ml;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
@NamedQuery(name="SomeDataClass.findSome", query="SELECT m FROM SomeDateClass m where m.van like :van")
public class SomeDataClass implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -4567908639347398145L;
long f1;
long f2;
String f3;
double f4;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public long getF1() {
    return f1;
}
public void setId(long f1) {
    this.f1 = f1;
}
public long getf2() {
    return f2;
}
public void setF2(long f2) {
    this.f2 = f2;
}
public String getF3() {
    return f3;
}
public void setF3(String f3) {
    this.van = f4;
}
public double getF4() {
    return f4;
}
public void setF4(double f4) {
    this.f4 = f4;
}
}

这是服务班

package de.ml;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Stateless
@LocalBean
@Path("/SomeService")
public class SomeService {
/*
 * 
 * http://localhost:8080/RestServiceExample5/matgi/SomeService/
 */
 @PersistenceContext(unitName="somePU",type=PersistenceContextType.TRANSACTION)
EntityManager em;
/**
 * Default constructor.
 */
public MatGi2() {

} 


/**
 * Retrieves representation of an instance of MatGi
 * 
 * @return an instance of String
*/
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public List<SomeDataClass> matgiGET(@FormParam("f1") String f1) {
    System.out.println("Beginn GET Method.");
    List<Materialgrundlagen> list = null;
    TypedQuery<Materialgrundlagen> query = em.createNamedQuery("Materialgrundlagen.findSome", Materialgrundlagen.class);
query.setParameter("van", "%"  + van + "%");
list=   query.getResultList();
System.out.println("End GET Method");


return list;
}

 /**
 * PUT method for updating or creating an instance of MatGi
 * 
 * @content content representation for the resource
 * @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes({ MediaType.APPLICATION_JSON })
public void resourceMethodPUT(SomeDataClass  content) {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
}
}

并了解不起作用的客户

package de.ml.restclient;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;

public class RestServiceExample5ClientJSON {
    public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client
            .target("http://hilweb05:8080/RestServiceExample5/matgi/SomeService/");
    Form form = new Form().param("f1", "value1");

    Response jsonAnswer = target.request()
            .accept(MediaType.APPLICATION_JSON).post(Entity.form(form));
    if (jsonAnswer.getStatus() != 200) {
        throw new RuntimeException("Hier laeuft was falsch : "
                + jsonAnswer.getStatus());
    }
    // Hier steigt er aus ...
    List<SomeDataClass> matList = jsonAnswer.readEntity(new  GenericType<List<SomeDataClass>>() {});
    for (SomeDataClass m : matList) {
        System.out.println(m.getF1() + " " + m.getF2() + " "
        + m.getF3());
    }       
}

}

共 (0) 个答案