Cómo arreglar 'HTTP 415' error, durante la solicitud POST en el servicio web REST utilizar inicio de primavera

Soumali Chatterjee:

Soy un principiante en la primavera de arranque y el aprendizaje a través de mi camino.

Cómo corregir 'HTTP 415' error, durante la solicitud POST en el servicio web REST utilizando Spring arranque de la siguiente manera? He tratado de @RequestMappinganotación, @RequestParam. @RequestParamda algún otro error 401. Sin embargo, 415 es consistente con @RequestMappingy @PostMapping.

Problemas con las @PostMappingpetición.

{
    "timestamp": "2018-12-31T18:29:36.727+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'text/plain;charset=UTF-8' not supported",
    "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 
    'text/plain;charset=UTF-8' not supported\r\n\tat 
    org.springframework.web.servlet.mvc.method.annotation.
    AbstractMessageConverterMethodArgumentResolver.
    readWithMessageConverters
    (AbstractMessageConverterMethodArgumentResolver.java:224)\r\n\tat 
     org.springframework.web.servlet.mvc.method.annotation.
     RequestResponseBodyMethodProcessor.
     readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)
     \r\n\tat org.springframework.web.servlet.mvc.method.
     annotation.RequestResponseBodyMethodProcessor.
     resolveArgument(RequestResponseBodyMethodProcessor.java:130)
     \r\n\tat...................

Mientras que la colocación de petición siguiente:

solicitud pOST

StudentController.java

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    :
    :

    @PostMapping("/students/{studentId}/courses")
    public ResponseEntity<Void> registerStudentForCourse(
            @PathVariable String studentId,
            @RequestBody Course newCourse) {
        Course course = studentService.addCourse(studentId, newCourse);

        if (course == null)
            return ResponseEntity.noContent().build();

        URI location = ServletUriComponentsBuilder.fromCurrentRequest().
                path("/{id}").buildAndExpand(course.getId()).toUri();

        return ResponseEntity.created(location).build();
    }

StudentService.java

@Component
public class StudentService {
    :
    :
    public Course addCourse(String studentId, Course course) {
        Student student = retrieveStudent(studentId);

        if (student == null) {
            return null;
        }

        String randomId = new BigInteger(130, random).toString(32);
        course.setId(randomId);

        student.getCourses().put(course.getId(), course);

        return course;
    }

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.in28minutes.springboot</groupId>
 <artifactId>student-services</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>student-services</name>
 <description>Demo project for Spring Boot</description>
 <properties>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
<build>
 <plugins>
     <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
 </plugins>
 </build>
</project>
Soumali Chatterjee:

Problema se soluciona mediante la adición de encabezado de contenido como application / json

introducir descripción de la imagen aquí

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=196858&siteId=1
Recomendado
Clasificación