500内部服务器错误 - Java webapp

时间:2015-05-29 11:54:15

标签: java mysql maven wildfly

我对Java webapp开发相对较新。我在开发应用程序时使用Maven,Wildfly和mySQL。

我被困了,因为当我在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)

1 个答案:

答案 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\"}";
    }