Lagom seems to be interesting but I'm having hard time to make something simple. It seems that I haven't understood how it works and the hello world example, although it works, I don't understand how to use it.
I'm trying to create a simple restful service that takes at its header two parameters and produces a json object. For instance in the MyService.java I have:
public interface BookService extends Service {
ServiceCall<NotUsed, String> getAllBook();
/**
* @return
*/
@Override
default Descriptor descriptor() {
return named("book").withCalls(
restCall(GET, "/api/get-all-book", this::getAllBook)
).withAutoAcl(true);
}
}
Then in BookServiceImpl I have:
public class BookServiceImpl implements BookService {
private final PersistentEntityRegistry persistentEntityRegistry;
/**
* @param registry
* @param readSide
* @param session
*/
@Inject
public BookServiceImpl(final PersistentEntityRegistry registry, ReadSide readSide, CassandraSession session) {
this.persistentEntityRegistry = registry;
persistentEntityRegistry.register(BookEntity.class);
readSide.register(BookEventProcessor.class);
}
@Override
public ServiceCall<NotUsed, String> getAllBook() {
return request -> {
JSONObject myBook= new JSONObject();
myBook.put("name","BookName");
myBook.put("description","A description");
myBook.put("price","$16");
myBook.put("status","available");
//how do I return JSONBject.toString()
};
}
}
And then how do I put headers parameters? Some documentation that explains basics would be very helpful.
Thanks in advance
You need to create a POJO class that actually does the JSON. By using the lombok package in service implementation:
package mybook;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import javax.annotation.concurrent.Immutable;
@Value
@Builder
@Immutable
@JsonDeserialize
@AllArgsConstructor
public class Book {
String name;
String description;
String value;
String status;
}
Then in service:
public interface BookService extends Service {
ServiceCall<NotUsed, Book> getAllBook();
/**
* @return
*/
@Override
default Descriptor descriptor() {
return named("book").withCalls(
restCall(GET, "/api/get-all-book", this::getAllBook)
).withAutoAcl(true);
}
}
And then in implementation:
public class BookServiceImpl implements BookService {
@Override
public ServiceCall<NotUsed, Book> getAllBook() {
return request -> {
Book myBook = Book.builder().
name("BookName").
description("A description").
price("16€").
status("available").build();
return completedFuture(myBook);
};
}
}