1

I write a rest ful web service that has formParam and return a list . and I test it in postman . but I get this error.HTTP Status 500.The server encountered an internal error that prevented it from fulfilling this request error. here is my service :

@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;
    }
}
}

I test Weighting.LoadWeightingInSpecTimeInSpecPlate(startTime, endTime, plate) and this work correctly . can eny one help me ?

stack trace :

Blockquote21-Aug-2015 17:44:31.133 WARNING [http-nio-8084-exec-197] org.glassfish.jersey.servlet.WebComponent.filterFormParameters A servlet request to the URI http://127.0.0.1:8084/fsc-access/rest/report/loadWeightingByPlate contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected. 21-Aug-2015 17:44:31.210 SEVERE [http-nio-8084-exec-197] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List.

now my service works well and I write a client to use this service but I get an error : HTTP 400 Bad Request :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));

how I can fix it ?

Fatemeh Moh
  • 129
  • 1
  • 5
  • 13

1 Answers1

1

First, the reason you're getting the 500 is told by the following message:

21-Aug-2015 17:44:31.210 SEVERE [http-nio-8084-exec-197] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=java.util.List.

That indicates that Jersey, your servlet, does not have a registered MessageBodyWriter that returns true from isWritable() with the following parameters

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

AFAIK Jersey usually has access to a JAXB implementation, but some JAXB implementations cannot handle generics like List correctly. What I would suggest is that you create a new class called Weightings that is a list wrapper for a collection of Weighting objects. This is a common thing to do with JAXB models.

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

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

Then modify your resource to return the new type:

@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;
    }
}

That should fix your 500 response. Your other option is to look around and test other JAXB or application/xml MessageBodyWriter implementations that might support generics better.

The other warning you're getting:

21-Aug-2015 17:44:31.133 WARNING [http-nio-8084-exec-197] org.glassfish.jersey.servlet.WebComponent.filterFormParameters A servlet request to the URI http://127.0.0.1:8084/fsc-access/rest/report/loadWeightingByPlate contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

I would take a look at this question if you're interested in fixing the warning.

Community
  • 1
  • 1
Samuel
  • 16,923
  • 6
  • 62
  • 75
  • thanks alot Samuel, this work well . now I write a client to use this service. but when I use it i receive a HTTP 400 Bad Request : javax.ws.rs.BadRequestException . I put my client source in my question . can you help me ? – Fatemeh Moh Aug 21 '15 at 16:22
  • Fatemeh, you should open a new question for your new question so that it gets more attention and is more likely to get answered – Samuel Aug 21 '15 at 17:51
  • ok, Samuel . I create a new question in : http://stackoverflow.com/questions/32147028/http-400-bad-request-javax-ws-rs-badrequestexception – Fatemeh Moh Aug 21 '15 at 18:29