spring restfull 服务请求和响应 Text/plan
spring restfull service request and response as Text/plan
我是 Spring 引导新手,我只是按照一个简单的教程完成了一个简单的服务,我收到了一个 Json 请求和一个 Json 响应,现在我需要将 request/response 更改为 Text/Plain,这是我控制器中的内容 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);
}
}
能否请您告诉我我必须更改哪些内容才能收到 Text/Plain 和具有相同媒体类型的响应。
在所有 Mapping 注释(@Get、@Post...)中,它们都有一个属性 consumes 和 produces,您可以使用媒体类型添加它
@PostMapping(value="/foo", consumes = MediaType.TEXT_PLAIN_VALUE , produces= MediaType.TEXT_PLAIN_VALUE)
public String plainValue(@RequestBody String data) {
return; //logic
}
我是 Spring 引导新手,我只是按照一个简单的教程完成了一个简单的服务,我收到了一个 Json 请求和一个 Json 响应,现在我需要将 request/response 更改为 Text/Plain,这是我控制器中的内容 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);
}
}
能否请您告诉我我必须更改哪些内容才能收到 Text/Plain 和具有相同媒体类型的响应。
在所有 Mapping 注释(@Get、@Post...)中,它们都有一个属性 consumes 和 produces,您可以使用媒体类型添加它
@PostMapping(value="/foo", consumes = MediaType.TEXT_PLAIN_VALUE , produces= MediaType.TEXT_PLAIN_VALUE)
public String plainValue(@RequestBody String data) {
return; //logic
}