尝试在postman上使用put和post方法到我的Spring rest应用程序时出现500内部服务器错误

时间:2018-06-05 09:54:11

标签: java spring rest post

我尝试了xml和json。使用POST时,它会向列表中添加null元素,并提供java.lang.NoClassDefFoundError异常。使用PUT时,我收到运行时异常。 GET方法正常运行。你能帮我调试代码吗? 这是我的服务和控制器类。

@Consumes("application/xml,application/json")
@Produces("application/xml,application/json")
@RestController
public class PatientController {

    @Autowired
    private PatientService patientService;

    @GetMapping("patientservice/patients")
    List<Patient> getPatients() {

        return patientService.getPatients();
    }

    @GetMapping("patientservice/patients/{id}")
    Patient getPatient(@PathVariable("id") Long id ) {
        return patientService.getPatient(id);
    }

    @PostMapping("patientservice/patients" )
    Response createPatient(Patient patient) {
        return patientService.createPatient(patient);
    }

    @PutMapping("patientservice/patients")
    Response updatePatient(Patient patient) {
        return patientService.updatePatient(patient);
    }
}

@Service
public class PatientServiceImpl implements PatientService {

    Map<Long, Patient> patients = new HashMap<>();
    Long currentId = new Long(123);

    public PatientServiceImpl() {
        init();
    }

    void init() {
        Patient patient = new Patient();
        patient.setId(currentId);
        patient.setName("John");
        Patient patient1 = new Patient();
        patient1.setId(new Long(++currentId));
        patient1.setName("Mike");
        Patient patient2 = new Patient();
        patient2.setId(new Long(++currentId));
        patient2.setName("Dave");
        patients.put(patient.getId(), patient);
        patients.put(patient1.getId(), patient1);
        patients.put(patient2.getId(), patient2);
    }

    @Override
    public List<Patient> getPatients() {
        Collection<Patient> results = patients.values();
        List<Patient> response = new ArrayList<>(results);
        return response;
    }

    @Override
    public Patient getPatient(Long id) {
        if (patients.get(id) == null) {
            throw new NotFoundException();
        }
        return patients.get(id);
    }

    @Override
    public Response createPatient(Patient patient) {
        patient.setId(++currentId);
        patients.put(patient.getId(), patient);
        return Response.ok(patient).build();
    }

    @Override
    public Response updatePatient(Patient patient) {

        Patient currentPatient = patients.get(patient.getId());

        Response response;
        if (currentPatient != null) {
            patients.put(patient.getId(), patient);
            response = Response.ok().build();
        } else {
            throw new PatientBusinessException();
        }
        return response;
    }

    @Override
    public Response deletePatient(Long id) {
        Patient patient = patients.get(id);

        Response response;

        if (patient != null) {
            patients.remove(id);
            response = Response.ok().build();
        } else {
            response = Response.notModified().build();
        }
        return response;
    }

}

的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>

    <groupId>com.bharath.restws</groupId>
    <artifactId>restws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>restws</name>
    <description>Patient REST Services</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.1.11</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-xc</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/logutil/logutil -->
        <!-- <dependency>
            <groupId>logutil</groupId>
            <artifactId>logutil</artifactId>
            <version>0.2.1</version>
        </dependency> -->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-common-utilities -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-common-utilities</artifactId>
            <version>2.0.6</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

4 个答案:

答案 0 :(得分:1)

您似乎错过了@RequestBody注释,以指明参数如何绑定到网络请求。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestBody.html

@PostMapping("patientservice/patients" )
Response createPatient(@RequestBody Patient patient) {
    return patientService.createPatient(patient);
}

答案 1 :(得分:0)

我不确定您是否正在构建apache-cfx项目的spring项目。但是如果你想使用spring-boot创建服务,这对你来说很有用。

PatientController.java

import org.springframework.web.bind.annotation.*;

@RestController
public class PatientController {



    @PostMapping("patientservice/patients" )
    Response createPatient(@RequestBody Patient patient) {
        System.out.println("createPatient"+patient.toString());
        return new Response("Sucess","200");
    }

    @PutMapping("patientservice/patients")
    Response updatePatient(@RequestBody Patient patient) {
        System.out.println("updatePatient"+patient.toString());
        return new Response("Sucess","200");
    }
}

Patient.java

public class Patient {

    String fName;
    String lName;
    String disease;

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public String getDisease() {
        return disease;
    }

    public void setDisease(String disease) {
        this.disease = disease;
    }

    @Override
    public String toString() {
        return "Patient{" +
                "fName='" + fName + '\'' +
                ", lName='" + lName + '\'' +
                ", disease='" + disease + '\'' +
                '}';
    }
}

在spring-boot的主要类中,您必须使用@ComponentScan("com.your.package")

之类的注释

的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>

    <groupId>com.your.package</groupId>
    <artifactId>patientservice</artifactId>
    <version>2.1.1</version>
    <packaging>war</packaging>

    <name>patientservice</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
        <spring.version>5.0.3.RELEASE</spring.version>
        <jackson.version>2.9.4</jackson.version>
        <hibernate.version>5.2.11.Final</hibernate.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.2.Final</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>patientservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

答案 2 :(得分:0)

看起来你错过了cxf-api依赖:

<dependency>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-api</artifactId>
 <version>2.2.3</version>  
</dependency>

答案 3 :(得分:0)

在pom.xml中添加以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.10.1</version>
</dependency>

面临与返回类型相同的问题,因此无法与耐心的MediaType绑定。添加依赖项后,它就起作用了。