Write Methods

Annotating a method with the Write annotation will cause it to respond to POST requests.

CurrentDateEndpoint

  1. import io.micronaut.management.endpoint.annotation.Endpoint;
  2. import io.micronaut.management.endpoint.annotation.Write;
  3. import io.micronaut.http.MediaType;
  4. import io.micronaut.management.endpoint.annotation.Selector;
  5. @Endpoint(id = "date",
  6. prefix = "custom",
  7. defaultEnabled = true,
  8. defaultSensitive = false)
  9. public class CurrentDateEndpoint {
  10. private Date currentDate;
  11. @Write
  12. public String reset() {
  13. currentDate = new Date();
  14. return "Current date reset";
  15. }
  16. }

CurrentDateEndpoint

  1. import io.micronaut.management.endpoint.annotation.Endpoint
  2. import io.micronaut.management.endpoint.annotation.Write
  3. import io.micronaut.http.MediaType
  4. import io.micronaut.management.endpoint.annotation.Selector
  5. @Endpoint(id = "date",
  6. prefix = "custom",
  7. defaultEnabled = true,
  8. defaultSensitive = false)
  9. class CurrentDateEndpoint {
  10. Date currentDate
  11. @Write
  12. String reset() {
  13. currentDate = new Date()
  14. return "Current date reset"
  15. }
  16. }

CurrentDateEndpoint

  1. import io.micronaut.management.endpoint.annotation.Endpoint
  2. import io.micronaut.management.endpoint.annotation.Write
  3. import io.micronaut.http.MediaType
  4. import io.micronaut.management.endpoint.annotation.Selector
  5. @Endpoint(id = "date", prefix = "custom", defaultEnabled = true, defaultSensitive = false)
  6. class CurrentDateEndpoint {
  7. private var currentDate: Date? = null
  8. @Write
  9. fun reset(): String {
  10. currentDate = Date()
  11. return "Current date reset"
  12. }
  13. }

The above method responds to the following request:

  1. $ curl -X POST http://localhost:39357/date
  2. Current date reset

The Write annotation accepts an optional consumes argument, which sets the media type accepted by the method (default is application/json):

MessageEndpoint

  1. import io.micronaut.context.annotation.Requires;
  2. import io.micronaut.management.endpoint.annotation.Endpoint;
  3. import io.micronaut.management.endpoint.annotation.Write;
  4. import io.micronaut.http.MediaType;
  5. @Endpoint(id = "message", defaultSensitive = false)
  6. public class MessageEndpoint {
  7. String message;
  8. @Write(consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.TEXT_PLAIN)
  9. public String updateMessage(String newMessage) {
  10. this.message = newMessage;
  11. return "Message updated";
  12. }
  13. }

MessageEndpoint

  1. import io.micronaut.management.endpoint.annotation.Endpoint
  2. import io.micronaut.management.endpoint.annotation.Write
  3. import io.micronaut.http.MediaType
  4. @Endpoint(id = "message", defaultSensitive = false)
  5. class MessageEndpoint {
  6. String message
  7. @Write(consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.TEXT_PLAIN)
  8. String updateMessage(String newMessage) { (1)
  9. message = newMessage
  10. return "Message updated"
  11. }
  12. }

MessageEndpoint

  1. import io.micronaut.context.annotation.Requires
  2. import io.micronaut.management.endpoint.annotation.Endpoint
  3. import io.micronaut.management.endpoint.annotation.Write
  4. import io.micronaut.http.MediaType
  5. @Endpoint(id = "message", defaultSensitive = false)
  6. class MessageEndpoint {
  7. internal var message: String? = null
  8. @Write(consumes = [MediaType.APPLICATION_FORM_URLENCODED], produces = [MediaType.TEXT_PLAIN])
  9. fun updateMessage(newMessage: String): String { (1)
  10. this.message = newMessage
  11. return "Message updated"
  12. }
  13. }

The above method responds to the following request:

  1. $ curl -X POST http://localhost:65013/message -H 'Content-Type: application/x-www-form-urlencoded' -d $'newMessage=A new message'
  2. Message updated