泽西RESTful ressource,500错误

时间:2015-08-13 01:38:18

标签: hibernate rest jaxb jersey jersey-client

我是RESTful网络服务的新手,我正在开发一种网络服务:

  1. 使用XML流(通过泽西客户端)
  2. 然后将XML编组为Java Object
  3. 最后使用hibernate持久保存这个java对象。
  4. 解组并且一切正常,但每当我在返回响应之前尝试访问服务层以使用“持久”方法时,我会收到 500错误的响应。

    即使我将整个hibernate代码放在Web服务主体中而没有访问服务层,我也会收到 500错误的响应。我不知道如何处理这个,你真的需要你的帮助。

    项目类:

    JAXB

    int function1(){
        json_t *jsonObj;
        function2(jsonObj);
        char * output = NULL;
        output = jsonToChar(jsonObj); //output is NULL after this, so jsonObj is probably empty
        ...
        return (0);
    }
    
    int *function2(json_t *jsonObj){
        DL_MY_MSG myMsg;
        //here I set myMsg correctly
    
        jsonObj = myObjToJson(&myMsg);
        char * output = NULL;
        output = jsonToChar(jsonObj); //output has the expected contents from jsonObj, so jsonObj is OK
    
        return (0);
    }
    
    json_t *myObjToJson(DL_MY_MSG *inputMessage){
        //converts obj to json_t and returns it
    }
    


    模型类

    @XmlRootElement(name = "student")
    public class Student {
    
    @XmlElement 
    private int id;
    @XmlElement
    private String firstName;
    @XmlElement
    private String lastName;
    @XmlElement
    private int age;
    
    public Student() { }
    
    // getters and setters
    
    } 
    


    StudentRest Class

    @Entity
    @Table(name = "student")
    public class StudentModel {
    @Id @GeneratedValue
    @Column(name="ID")
    private int id;
    
    @Column(name="FIRSTNAME")
    private String firstName;
    
    @Column(name="LASTNAME")
    private String lastName;
    
    @Column(name="AGE")
    private int age;
    
    public StudentModel() { }
    
    // getters and setters
    
    } 
    


    jerseyClient Class

    @Path("/xmlServices")
    public class StudentRest{   
    
    @POST
    @Path("/send")
    @Consumes(MediaType.APPLICATION_XML)
    
    public Response consumeXML( Student student ) {
       // JAXB TO MODEL
      StudentModel model=new StudentModel();
      model.setFirstName(student.getFirstName());
      model.setLastName(student.getLastName());
      model.setAge(student.getAge());
    
           Session session = null;
           Transaction tx = null;
           session = HibernateUtil.getSessionFactory().openSession();
           tx = session.beginTransaction();
    
           session.save(model);
    
           tx.commit();
           session.close();
    
    
    
         String output = student.toString();
    
        return Response.status(200).entity(output).build();
    }
    }
    


    HelpMeExceptionMapper类

     public class JerseyClient {
    
    public static void main(String[] args) {
        try {
    
            Student st = new Student("Adriana", "Barrer", 12, 9);
    
            Client client = Client.create();
    
            WebResource webResource = client
                    .resource("http://localhost:8080/JerseyXMLExample/rest/xmlServices/send");
    
            ClientResponse response = webResource.accept("application/xml")
                    .post(ClientResponse.class, st);
    
            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }
    
            String output = response.getEntity(String.class);
    
            System.out.println("Server response : \n");
            System.out.println(output);
    
        } catch (Exception e) {
    
            e.printStackTrace();
    
        }
    
    }
    
    }  
    


    堆栈追踪:

     @Provider
     public class HelpMeExceptionMapper implements ExceptionMapper<Exception>{
    
     public Response toResponse(Exception e) {
        e.printStackTrace();
        return Response
                    .status(Status.INTERNAL_SERVER_ERROR)
                    .type(MediaType.APPLICATION_XML)
                    .entity(e.getCause())
                    .build();
    }
    
    
    }
    

0 个答案:

没有答案