POST格式

时间:2016-07-11 13:41:22

标签: java exception post request client

Restfull客户端无法正常工作

我在服务器上的Restfull服务运行良好。我使用表单调用服务,然后以JSON格式获得漂亮的响应。但是,如果我用服务的主方法编写客户端,客户端将无法工作。

以下客户端代码

的例外情况
List<Materialgrundlagen> matList = jsonAnswer.readEntity(new   GenericType<List<Materialgrundlagen>>() {});

异常

找不到媒体类型= application / json的typeBodyReader,type = interface java.util.List,genericType = java.util.List。

我使用以下组件

  1. Glassfish 4.1.1
  2. 来自EclipseLink的最新OSGI捆绑包
  3. Jersy whis包含在模块
  4. 下的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 Entity和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 个答案:

没有答案