org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization

萝らか妹 提交于 2019-12-04 19:43:02

I was able to solve this issue. you need to make some changes in mapping request URL. I wonder how code in the block is working without error.

As per my understanding one possible cause is that you have two or more applicable mappings for that URL call. CustomerRest.java and ProductRest.java both were mapped to /.

@Path("/")

And

@Path("/")

The Jersey have no way of telling what method is actually supposed to be called and gives this error.

You need to correct following files: OrderRest.java

@Named
@Path("/")
public class OrderRest {
    private long id = 1;

    @Inject
    private RestTemplate restTemplate;

    @GET
    @Path("order")
    @Produces(MediaType.APPLICATION_JSON)
    public Order submitOrder(@QueryParam("idCustomer") long idCustomer,
            @QueryParam("idProduct") long idProduct,
            @QueryParam("amount") long amount) {

        Customer customer = restTemplate.getForObject("http://localhost:8080/customer/getCustomer?id={id}", Customer.class, idCustomer);
        Product product = restTemplate.getForObject("http://localhost:8080/product/getProduct?id={id}", Product.class,idProduct);

        Order order = new Order();
        order.setCustomer(customer);
        order.setProduct(product);
        order.setId(id);
        order.setAmount(amount);
        order.setOrderDate(new Date());
        id++;
        return order;
    }
}

In CustomerRest.java - use this

@GET
    @Path("/getCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    public Customer getCustomer(@QueryParam("id") long id) {
        Customer cli = null;
        for (Customer c : customers) {
            if (c.getId() == id)
                cli = c;
        }
        return cli;
    }

In ProductRest - use this

@GET
    @Path("/getProduct")
    @Produces(MediaType.APPLICATION_JSON)
    public Product getProduct(@QueryParam("id") long id) {
        Product prod = null;
        for (Product p : products) {
            if (p.getId() == id)
                prod = p;
        }
        return prod;
    }

Here is the output:

Try this if you have post method: I faced the same issue. But my usecase was bit different in that POST method passing 2 params thats why it's throwing the same exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!