I'm learning JAX-RS by making sample webapp, and I have a question about parameters injection from HTTP request to java method's parameters.
Now, I'm trying to inject query parameters into some beans on sample JAX-RS project , named 'cxftest01' using Apache CXF , which is supposed as HR management in a company.
So, I got started to make some beans classes following class 'Employee'
package com.sample.cxftest01.beans;
public class Employee {
private Name name;
private String division;
public Name getName() { return name; }
public void setName(Name name) { this.name = name; }
public String getDivision() { return division; }
public void setDivision(String division) { this.division = division; }
}
and next class 'Name'
package com.sample.cxftest01.beans;
public class Name {
private String first;
private String last;
public String getFirst() { return first; }
public void setFirst(String first) { this.first = first; }
public String getLast() { return last; }
public void setLast(String last) { this.last = last; }
}
and JAX-RS resouce class is next.
package com.sample.cxftest01.restapi;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
import com.sample.cxftest01.beans.Employee;
@Component
@Path("employees")
public class EmployeeResource {
@GET
@Produces("application/json")
public Response getPerson(@QueryParam("") Employee employee)
{
return Response.ok(employee, MediaType.APPLICATION_JSON ).build();
}
}
These codes above worked. When I accessed URL,
http://localhost:8080/cxftest01/employees?
name.first=Joe&name.last=Hayek&division=SoftwareDevelopment
on address bar in my web borowser(I use latest Chrome), the browser displayed next JSON.
{"name":{"first":"Joe","last":"Hayek"},"division":"SoftwareDevelopment"}
Next, I planed following senario. If I'll give 2 employees in query parameters like
name.first=Joe&name.last=Hayek&division=SoftwareDevelopment&
name.first=Meg&name.last=Tanaka&division=Sales
then I'll be glad to get next JSON as result,
[
{"name":{"first":"Joe","last":"Hayek"},"division":"SoftwareDevelopment"},
{"name":{"first":"Meg","last":"Tanaka"},"division":"Sales"}
]
So, I modified the method EmployeeResource#getPerson() as following:
@GET
@Produces("application/json")
public Response getPersons(@QueryParam("") List<Employee> employees)
{
return Response.ok(employees, MediaType.APPLICATION_JSON ).build();
}
And I tried to access next URL:
http://localhost:8080/cxftest01/employees?
name.first=Joe&name.last=Hayek&division=SoftwareDevelopment&
name.first=Meg&name.last=Tanaka&division=Sales
, but the borowser displayed next error message.
Class java.util.List can not be instantiated
So I ask next two points.
1) What is the reason why the error indicated by the message above was happened ?
2) If you know how to inject query parameters to beans list or array as a
parameter of some methods in JAX-RS resouce class, as my senario
explained above, please teach me.
Best regards.