2015-05-29 148 views
0

我對Java webapp開發相對較新。我在開發應用程序時使用Maven,Wildfly和mySQL。500內部服務器錯誤 - Java webapp

我被卡住了,因爲當我在webapp中輸入數據時,它不會被保存到數據庫中。我試着用Firebug來識別錯誤,狀態碼是'500內部錯誤'。我在WEB-INF和JAX-RS註釋中有我的beans.xml,我似乎無法識別問題。請幫忙。

我的beans.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> 
</beans> 

我的對象類是:

package com.Malison.job.model; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Table; 
import javax.ws.rs.FormParam; 


import com.Malison.common.model.BaseEntity; 


@Entity 
@Table 
public class Job extends BaseEntity { 
private static final long serialVersionUID = 1L; 

@FormParam ("client") 
@Column 
private String client; 

@FormParam ("location") 
@Column (name = "Loaded_From") 
private String loadedFrom; 

@FormParam ("destination") 
@Column 
private String destination; 

@Column 
private int distance; 

@FormParam ("product") 
@Column 
private String product; 

@FormParam ("quantity") 
@Column (name = "Quantity_Loaded") 
private int quantityloaded; 

@FormParam ("rate") 
@Column (name = "Rate_of_Transport") 
private int rateOfTransport; 

@Column 
private int amount = rateOfTransport * distance; 

@FormParam ("delivery") 
@Column (name = "Delivery_Note_Number") 
private int deliveryNoteNo; 


@XmlElement (name = "distance") 
public double getDistance() { 
    return distance; 
} 

public void setDistance(int distance) { 
    this.distance = distance; 
} 

public String getClient() { 
    return client; 
} 


public void setClient(String client) { 
    this.client = client; 
} 


public String getProduct() { 
    return product; 
} 


public void setProduct(String product) { 
    this.product = product; 
} 
public double getAmount() { 
    return amount; 
} 


public String getLoadedFrom() { 
    return loadedFrom; 
} 

public void setLoadedFrom(String loadedFrom) { 
    this.loadedFrom = loadedFrom; 
} 


public String getDestination() { 
    return destination; 
} 

public void setDestination(String destination) { 
    this.destination = destination; 
} 


public long getQuantityloaded() { 
    return quantityloaded; 
} 

public void setQuantityloaded(int quantityloaded) { 
    this.quantityloaded = quantityloaded; 
} 

public long getRateOfTransport() { 
return rateOfTransport; 
} 

public void setRateOfTransport(int rateOfTransport) { 
    this.rateOfTransport = rateOfTransport; 
} 


public long getDeliveryNoteNo() { 
    return deliveryNoteNo; 
} 

public void setDeliveryNoteNo(int deliveryNoteNo) { 
    this.deliveryNoteNo = deliveryNoteNo; 
} 




} 

而且我的執着類:

package com.Malison.job.restws; 



import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.PersistenceUnit; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.core.MediaType; 




import org.jboss.resteasy.annotations.Form; 




@Path("/job") // http://localhost:8085/malison/api/job 
public class JobApi { 

    @PersistenceUnit 
    private EntityManagerFactory emf; 

    @POST 
    @Path("/create") // http://localhost:8085/malison/api/job/create 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    public String create(@Form com.Malison.job.model.Job job){ 

     EntityManager em = emf.createEntityManager(); 
     try { 
      em.getTransaction().begin(); 
      em.merge(job); 
      em.getTransaction().commit(); 
     } catch (Exception e) { 
      em.getTransaction().rollback(); 
      return "{\"success\":false, \"msg\":\"Error occured, please try later\"}"; 
     } 

     return "{\"success\":true, \"msg\": \"Saved successfully\"}"; 

    } 


} 

下面的消息在Firebug的日誌給出:

錯誤處理請求 上下文路徑: /malison

Servlet Path: 
/api 

Path Info: 
/job/create 

Query String: 
null 

Stack Trace 
org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalStateException: Transaction not active 
org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) 
org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) 
org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) 
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) 
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) 
org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) 
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) 
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) 
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) 
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) 
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) 
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) 
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63) 
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) 
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) 
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) 
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) 
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:247) 
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:76) 
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:166) 
io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) 
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
java.lang.Thread.run(Thread.java:745) 
+0

請分享您的錯誤消息。 –

+0

你試過看Firebug日誌嗎? – Mnemonics

+0

我看了一下螢火蟲日誌,它顯示以下消息 –

回答

1

該錯誤是因爲這

org.jboss.resteasy.spi.UnhandledException的:java.lang.IllegalStateException:事務不活躍

交易尚未設置。因此,如果你想讓代碼運行。您有兩種選擇:

  1. 使用容器管理的事務通過更改代碼來獲取管理器。或者,

    @PersistenceContext 
    EntityManager em; 
    
  2. 應用管理的事務

    @PersistenceUnit 
    private EntityManagerFactory emf; 
    
    @Resource 
    private UserTransaction utx; 
    
    @POST 
    @Path("/create") // http://localhost:8085/malison/api/job/create 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    public String create(@Form com.Malison.job.model.Job job){ 
    
        EntityManager em = emf.createEntityManager(); 
        try { 
         utx.begin(); 
         em.merge(job); 
         utx.commit(); 
        } catch (Exception e) { 
         utx.rollback(); 
         return "{\"success\":false, \"msg\":\"Error occured, please try later\"}"; 
        } 
        return "{\"success\":true, \"msg\": \"Saved successfully\"}"; 
    }