使用 laggom 创建一个简单的模拟 json 服务

Lagom 似乎很有趣,但我很难让事情变得简单。好像没看懂怎么用和hello world的例子,虽然可以用,但是不明白怎么用。


我正在尝试创建一个简单的 restful 服务,该服务在其标头中接受两个参数并生成一个 json 对象。例如在 MyService.java 我有:


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);

    }

}

然后在 BookServiceImpl 我有:


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()

        };

    }

}

然后我如何放置标题参数?一些解释基础知识的文档将非常有帮助。


提前致谢


慕哥6287543
浏览 98回答 1
1回答

慕婉清6462132

您需要创建一个实际执行 JSON 的 POJO 类。通过在服务实现中使用 lombok 包: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@AllArgsConstructorpublic class Book {&nbsp; &nbsp; String name;&nbsp;&nbsp; &nbsp; String description;&nbsp;&nbsp; &nbsp; String value;&nbsp;&nbsp; &nbsp; String status;}然后在服务中:public interface BookService extends Service {&nbsp; &nbsp; ServiceCall<NotUsed, Book> getAllBook();&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; default Descriptor descriptor() {&nbsp; &nbsp; &nbsp; &nbsp; return named("book").withCalls(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restCall(GET, "/api/get-all-book", this::getAllBook)&nbsp; &nbsp; &nbsp; &nbsp; ).withAutoAcl(true);&nbsp; &nbsp; }}然后在实施中:public class BookServiceImpl implements BookService {&nbsp; &nbsp; @Override&nbsp; &nbsp; public ServiceCall<NotUsed, Book> getAllBook() {&nbsp; &nbsp; &nbsp; &nbsp; return request -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Book myBook =&nbsp; &nbsp; &nbsp;Book.builder().&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name("BookName").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description("A description").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price("16€").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status("available").build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return completedFuture(myBook);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java