Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF 2.0 and REST(Jeresy) Integratio

 
Pawan Komaram
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What are the ways to integrate JSF 2.0 with REST, here I want to use Jersey implimentation of REST. The idea here is to get the response from REST webservice as JSON and use it in the JSF. What are best ways to achieve this? Can any one give simple log in kind of example to achieve this?

Thanks
 
Brijesh Verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two steps

just create interface class and map logical url to resource class

@Path(EmployeeResource.EMP_URL)

interface EmployeeClient{

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("all")
public ClientResponse<Employee> getEmployeeDetails();

}


@Path( EmployeeResource.EMP_URL)
class EmployeeResource{
public static final String EMP_URL = "/service/employee";

@Autowired
EmployeeService. employeeService.;


@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("all")
public Employee getEmployeeDetails(){

return employeeService.getEmployee();

}

finally initialize client in resteasy.xml


now you can use client directly in jsf handler class


 
Pawan Komaram
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how to call getEmployeeDetails() method of EmployeeClient interface from my Managed bean class in JSF and what is the fromate of retun value by getEmployeeDetails() method? wil it be a ClientResponse<Employee> object or JSON? if it is java object then what is the use of having MediaType.APPLICATION_JSON property. If JSON is produced from the method call then how to inject that JSON data into my JSF page from my Manged bean (handler class).
 
Brijesh Verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to call client object from you jsf handler class and return type is ClientResponse<Users> in your client class

Remember Users class is not your domain class . it is your bean class which you have create because web service does not understand List<User>

So follow these steps
UserHandler class


@Controller("userHandler")
@Scope("session")
public class UserHandler implements Serializable {

@Resource
private UserClient userClient;

private List<User> users;

public Collection<User> getUsers() {
if (users == null) {

users = userClient.getUsers().getEntity().getUsers();

}
return users;
}

And use this bean class in your client class for return type

@XmlRootElement
public class Users {


private List<User> users = new ArrayList<User>();


public Users() {
}

public Users(List<User> users) {
this.users = users;
}


@XmlElement(name = "user")
public List<User> getUsers() {
return users;
}


public void setUsers(List<User> users) {
this.users = users;
}
}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic