Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin - REST example

Nazar Medeiros
5 years ago Oct 06, 2016 12:56pm
Tobias Demuth
5 years ago Oct 06, 2016 3:06pm

Status Code 500 will always go hand in hand with some kind of stacktrace. Could you please provide this crucial information?

Nazar Medeiros
5 years ago Oct 07, 2016 6:49am
Tobias Demuth
5 years ago Oct 07, 2016 7:18am
Nazar Medeiros
5 years ago Oct 07, 2016 7:31am
Tobias Demuth
5 years ago Oct 07, 2016 8:00am
Nazar Medeiros
5 years ago Oct 07, 2016 9:42am
Nazar Medeiros
5 years ago Oct 09, 2016 10:52am

Tobias Demuth: Next problem might be the fact that you are requesting a Customer but actually receiving a List of Customers. As far as I remember, it is probably better to use array here and to pass the correct class-type into get().

(new Customer[0]).getClass();

Finally I could solve the problem. There were few steps I had to do.  So let's start with my model class Customer.java

public class Customer {

    private String firstname;
    
    private String lastName;
    
    private Long id;
    
    public Customer() {
        
    }
    
    public Customer(Long id,String firstname,String lastname) {
        
        this.firstname = firstname;
        this.lastName = lastname;
        this.id = id;
        
    }
    
    @Override
    public String toString() {
        return "Customer [firstname=" + firstname + ", lastName=" + lastName + ", id=" + id + "]";
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

When using rest services with a content-type like json or xml you have to be sure, that your model class can be converted to the specific format. In my case I decided to use xml. Therefore I had to use the annotation @XmlRootElement. You only need to put this annotation at the top of your class.. nothing more needed.

The next step was to edit my Rest-Client.java:

public List<Customer>findByName(String firstname,String lastname) {
        
        List<Customer>customerList = new ArrayList<Customer>();
        
        customerList = webtarget.path("server").
                            path("restserver").
                            path("findByName").
                            queryParam("firstname", firstname).
                            queryParam("lastname", lastname).  
                            request().
                            accept(MediaType.APPLICATION_XML).
                            get(new GenericType<List<Customer>>(){});  
        return customerList;
        
    }

I expect an xml content-type from the server. This is the reason why I use accept(MediaType.APPLICATION_XML). In case you receive a list of items, you could use GenericType<List<Customer>>(){};

The fnal part - My Server.java:

@Path("/restserver")
public class Server {

    @Path("/findByName")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public GenericEntity<List<Customer>>findByName(@QueryParam("firstname")String firstname,@QueryParam("lastname") String lastname) throws ClassNotFoundException, SQLException
    {
        List<Customer>customerList = DBConnection.findByName(firstname, lastname);
        
         GenericEntity<List<Customer>>
            list = new GenericEntity<List<Customer>>(customerList) {
         };
         return list;
    }

Nothing special here. Just looking for some data in the database and returning them as a type of List<Customer>.
I hope this information could be helpful for someone.
Best regards,
Nazar Medeiros