C
C
click_f2016-07-20 15:58:52
Java
click_f, 2016-07-20 15:58:52

What is the minimum set of JAVA tools for working with a REST API service?

It is necessary to accept and process POST, GET requests in JSON format.

  • Is it possible to do without controllers?
  • What libraries should be used?
  • How can this be done as simply as possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kosarev, 2016-07-20
@click_f

Is it possible to do without controllers?

It is possible, but with them it is easier and more correct. And in general, what are the arguments against?
Java EE (Jersey for example) or Spring Web
How to implement this as simply as possible?
On Spring:
@RestController
@RequestMapping("/books")
public class BooksService {
    @Autowired
    private BooksRepository booksRepository;

    /**
     *  Список книг
     */
    @RequestMapping
    public ResponseEntity list() {
        return ResponseEntity.ok(booksRepository.findAll());
    }

    /**
     *  Добавление новой книги
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity create(@RequestBody Book book) {
        return ResponseEntity.ok(booksRepository.save(book));
    }

    /**
     *  Получение существующей книги
     */
    @RequestMapping("/{id}")
    public ResponseEntity get(@PathVariable String id) {
        if (bookRepository.exists(id)) {
            return ResponseEntity.ok(booksRepository.save(book));
        }

        return ResponseEntity.notFound().build();
    }
}

In this case, Spring Data JPA is also used.

O
OnYourLips, 2016-07-20
@OnYourLips

Is Spring required in a task?
There is a very simple and lightweight Play framework. Preferred Scala language.
nordicapis.com/building-a-rest-api-in-java-scala-u...

import play.api._
import play.api.mvc._
 
object Application extends Controller {
   def getName = Action {
  	Ok("Jim")
  }
}

It is possible by passing the closure with the necessary code to the router, but this will not simplify the code.
PS There is a very tiny framework - Spark: sparkjava.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question