Gaston de la cruz
Gaston de la cruz

Reputation: 149

spring restful service request and response as text/plain

I'm new in Spring boot, I just follow a simple tutorial and I finished a simple service where I received a Json request and Response with a Json as well, now I need to change the request/response as a Text/Plain, these its what I have in my controller class:

package com.notas.core.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.notas.core.entity.Nota;
import com.notas.core.model.MNota;
import com.notas.core.service.NotaService;

@RestController
@RequestMapping("/v1")
public class NotaController {

    @Autowired
    @Qualifier("servicio")
    NotaService servicio;

    @PutMapping("/nota")
    public boolean agregarNota(@RequestBody @Valid Nota nota) {
        return servicio.crear(nota);
    }

    @PostMapping("/nota")
    public boolean modificarNota(@RequestBody @Valid Nota nota) {
        return servicio.actualizar(nota);
    }

    @DeleteMapping("/nota/{id}/{nombre}")
    public boolean borrarNota(@PathVariable("id") long id, @PathVariable("nombre") String nombre) {
        return servicio.borrar(nombre, id);
    }

    @GetMapping("/notas")
    public List<MNota> obtenerNotas(Pageable pageable){
        return servicio.obtenerPorPaginacion(pageable);
    }
}

Could you please tell me whats do I have to change in order to receive a Text/Plain and response with the same mediatype.

Upvotes: 1

Views: 524

Answers (1)

Avi
Avi

Reputation: 1528

In all Mapping annotation (@Get, @Post...) they have an attribute consumes and produces, you could add that using the media type

@PostMapping(value="/foo", consumes = MediaType.TEXT_PLAIN_VALUE , produces= MediaType.TEXT_PLAIN_VALUE)
public String plainValue(@RequestBody String data) {
   return; //logic
}

Upvotes: 3

Related Questions