简体   繁体   中英

HTTP 500 error failed with 200 status code

I am have developed a jax-rs project and called to a REST service to add data to a database. When I am sending a POST request to add some data, it gives 200 status code, adds data to the database and additionally it gives 500 status which says internal server error.

Below is my jax-rs code.

@POST
@Path("/users/add/")
@Produces("text/plain")
public Response addUser(
        @QueryParam("userId") String userId,
        @QueryParam("userName") String userName,
        @QueryParam("userRole") String userRole,
        @QueryParam("email") String email,
        @QueryParam("password") String password,
        @QueryParam("phone") String phone) {

    System.out.println("----invoking addUser");

    try{
        httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://appserver.dev.cloud.wso2.com/services/t/madusanka/projecttrackerdb-default-SNAPSHOT/addUser");
        postRequest.addHeader("accept", "application/xml");

        List nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("userId",userId));
        nameValuePairs.add(new BasicNameValuePair("userName",userName));
        nameValuePairs.add(new BasicNameValuePair("userRole",userRole));
        nameValuePairs.add(new BasicNameValuePair("email",email));
        nameValuePairs.add(new BasicNameValuePair("password",password));
        nameValuePairs.add(new BasicNameValuePair("phone",phone));
        postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse response = httpClient.execute(postRequest);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 201)
        {
            throw new RuntimeException("Failed with HTTP error code : " + statusCode);
        }
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        //Important: Close the connect
        httpClient.getConnectionManager().shutdown();
    }
    return Response.ok().build();
}

And I got the following response when I call it from REST client.

Status : 500 Internal Server Error
org.apache.cxf.interceptor.Fault: Failed with HTTP error code : 200
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:194)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:100)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:57)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:93)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:159)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:206)
javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)

What could be the reason for this?

You check the status using 201 . You can change it to 200 .

    if (statusCode != 200)
    {
        throw new RuntimeException("Failed with HTTP error code : " + statusCode);
    }

It's better to use a rang instead of just one number to check HTTP failure:

    if (statusCode > 399)
    {
        throw new RuntimeException("Failed with HTTP error code : " + statusCode);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM