I do rest service. The task is to transfer the Json array and save it in the database.
At the moment so:
MessageController
//Add one message @RequestMapping( value = "/api/message", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> addMessage(@RequestBody Message newMessage) { return new ResponseEntity<>(messageRepository.save(newMessage), HttpStatus.CREATED); } //Add array of messages @RequestMapping( value = "api/messages", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> addMessages(@RequestBody List<Message> newMessages) { return new ResponseEntity<Object>(messageRepository.save(newMessages), HttpStatus.CREATED); } Both one element and the Json array are stored. Next, I want to enter the service layer.
public interface MessageService { Message save(Message message); Message saveArray(List<Message> messages); @Service public class MessageServiceImp implements MessageService{ private MessageRepository messageRepository; @Override public Message save(Message message) { return messageRepository.save(message); } @Override public Message saveArray(List<Message> messages) { return messageRepository.save(messages); //а тут я уже запутался } Why does the save method iterate over the array directly in the controller, and it turns out that it doesn't? What should it look like?
Added by:
@Entity @Table(name = "message") public class Message { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private long id; @Column(name = "MESSAGE") private String message; @Column(name = "AUTHOR") private String author; @Column(name = "CREATED") @Temporal(TemporalType.DATE) private Date created = new Date(); public Message() { } public Message(Long id, String message, String author, Date created) { this.id = id; this.message = message; this.author = author; this.created = created; } + getters / setters
Message? What is happening in the place//а тут я уже запутался? An exception? If so, which one? - MaksimMessageto the service class - Maksim