簡體   English   中英

靜態Web服務:HTTP狀態500。服務器遇到內部錯誤,阻止其滿足此請求錯誤

[英]restful web services: HTTP Status 500.The server encountered an internal error that prevented it from fulfilling this request error

我編寫了一個具有formParam的Restful Web服務,並返回一個列表。 然后我在郵遞員那里測試。 但我收到此錯誤。HTTP狀態500。服務器遇到內部錯誤,無法實現此請求錯誤。 這是我的服務:

@Path("/report")
public class weightingResource {

@POST
@Path("/loadWeightingByPlate")
//@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public List<Weighting> LoadWeightingInSpecTimeInSpecPlate(
        @FormParam("plate") String plate,
        @FormParam("startTime") String _startTime,
        @FormParam("endTime") String _endTime,
        @Context HttpServletRequest req) {
    Long startTime = new Long(_startTime);
    Long endTime = new Long(_endTime);
    try {
        List<Weighting> weightings = Weighting.LoadWeightingInSpecTimeInSpecPlate(startTime, endTime, plate);
        System.out.println("no error");
        return weightings;
    } catch (Exception ex) {
        System.out.println("Exception = " + ex);
        return null;
    }
}
}

我測試了Weighting.LoadWeightingInSpecTimeInSpecPlate(startTime, endTime, plate) ,它可以正常工作。 可以幫我嗎?

堆棧跟蹤 :

Blockquote21-Aug-2015 17:44:31.133警告[http-nio-8084-exec-197] org.glassfish.jersey.servlet.WebComponent.filterFormParameters對URI的Servlet請求http://127.0.0.1:8084/fsc -access / rest / report / loadWeightingByPlate在請求正文中包含表單參數,但是該請求正文已被servlet或訪問請求參數的servlet過濾器消耗。 只有使用@FormParam的資源方法才能按預期工作。 通過其他方式消耗請求主體的資源方法將無法正常工作。 2015年8月21日17:44:31.210嚴重[http-nio-8084-exec-197] org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter找不到媒體類型= application / xml,type =類java.util.ArrayList,genericType = java.util.List。

現在我的服務運行良好,並且我編寫了一個客戶端來使用該服務,但出現錯誤:HTTP 400錯誤請求:javax.ws.rs.BadRequestException

        String webserviceURI = "http://localhost:8084/fsc-access";

    ClientConfig clientConfig = new ClientConfig();
    Client client = ClientBuilder.newClient(clientConfig);
    URI serviceURI = UriBuilder.fromUri(webserviceURI).build();
    WebTarget webTarget = client.target(serviceURI);
    MultivaluedMap formData = new MultivaluedMapImpl();
    formData.add("plate", plate);
    formData.add("startTime", start.toString());
    formData.add("endTime", end.toString());
    Weightings weightings = new Weightings();
     weightings.getWeightings().addAll((Collection<? extends Weighting>) webTarget.path("rest").path("report").path("loadWeightingByPlate").
            request().accept(MediaType.APPLICATION_XML).post(javax.ws.rs.client.Entity.form(formData), Weightings.class));

我該如何解決?

首先,以下消息告訴您獲得500的原因:

2015年8月21日17:44:31.210嚴重[http-nio-8084-exec-197] org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter找不到媒體類型= application / xml,type =類java.util.ArrayList,genericType = java.util.List。

這表示您的servlet澤西島沒有注冊的MessageBodyWriter ,該消息從isWritable()返回true帶有以下參數

media type=application/xml, 
type=class java.util.ArrayList, 
genericType=java.util.List

AFAIK Jersey通常可以訪問JAXB實現,但是某些JAXB實現無法正確處理諸如List泛型。 我建議您創建一個名為Weightings的新類,該類是Weighting對象集合的列表包裝。 這對於JAXB模型是很常見的事情。

@XmlRootElement
public class Weightings {
    @XmlElement
    private final List<Weighting> weightings = new ArrayList<Weighting>();

    public List<Weighting> getWeightings() {
        return weightings;
    }
}

然后修改您的資源以返回新類型:

@POST
@Path("/loadWeightingByPlate")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Weightings LoadWeightingInSpecTimeInSpecPlate(
        @FormParam("plate") String plate,
        @FormParam("startTime") String _startTime,
        @FormParam("endTime") String _endTime,
        @Context HttpServletRequest req) {
    Long startTime = new Long(_startTime);
    Long endTime = new Long(_endTime);
    try {
        Weightings weightings = new Weightings();
        weightings.getWeightings().addAll( Weighting.LoadWeightingInSpecTimeInSpecPlate(startTime, endTime, plate));
        System.out.println("no error");
        return weightings;
    } catch (Exception ex) {
        System.out.println("Exception = " + ex);
        return null;
    }
}

那應該可以解決您的500條回復。 您的另一個選擇是環顧四周並測試可能更好地支持泛型的其他JAXB或application/xml MessageBodyWriter實現。

您收到的另一個警告是:

2015年8月21日17:44:31.133警告[http-nio-8084-exec-197] org.glassfish.jersey.servlet.WebComponent.filterFormParameters對URI的Servlet請求http://127.0.0.1:8084/fsc -access / rest / report / loadWeightingByPlate在請求正文中包含表單參數,但是該請求正文已被servlet或訪問請求參數的servlet過濾器消耗。 只有使用@FormParam的資源方法才能按預期工作。 通過其他方式消耗請求主體的資源方法將無法正常工作。

如果您有興趣解決此警告,我將看看這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM