Java 尝试在我的SpringREST应用程序的postman上使用put和post方法时出现500内部服务器错误
我尝试了xml和json。使用POST时,它会将null元素添加到列表中,并给出java.lang.NoClassDefFoundError异常。使用PUT时,我收到一个运行时异常。GET方法正在正常运行。你能帮我调试代码吗? 以下是我的服务和控制器类Java 尝试在我的SpringREST应用程序的postman上使用put和post方法时出现500内部服务器错误,java,spring,rest,post,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 Pa
@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;
}
}
@使用(“应用程序/xml,应用程序/json”)
@生成(“应用程序/xml、应用程序/json”)
@RestController
公共类病人控制员{
@自动连线
私人病人服务;
@GetMapping(“患者服务/患者”)
病人名单(){
return patientService.getPatients();
}
@GetMapping(“patientservice/patients/{id}”)
患者getPatient(@PathVariable(“id”)长id){
返回patientService.getPatient(id);
}
@后映射(“患者服务/患者”)
响应createPatient(患者){
return patientService.createPatient(患者);
}
@PutMapping(“患者服务/患者”)
响应更新患者(患者){
返回patientService.updatePatient(患者);
}
}
@服务
公共类PatientServiceImpl实现PatientService{
Map patients=newhashmap();
Long currentId=新长(123);
公共病人服务中心{
init();
}
void init(){
病人=新病人();
患者设置ID(当前ID);
患者名称(“约翰”);
患者1=新患者();
patient1.setId(新长(++currentId));
患者1.设置名称(“迈克”);
患者2=新患者();
patient2.setId(新长(++currentId));
患者2.设置名称(“Dave”);
patients.put(patient.getId(),patient);
patients.put(patient1.getId(),patient1);
patients.put(patient2.getId(),patient2);
}
@凌驾
公众病人名单(){
收集结果=patients.values();
列表响应=新的ArrayList(结果);
返回响应;
}
@凌驾
公共患者getPatient(长id){
if(patients.get(id)==null){
抛出新的NotFoundException();
}
返回患者。获取(id);
}
@凌驾
公众响应创建患者(患者){
患者设置ID(++currentId);
patients.put(patient.getId(),patient);
返回Response.ok(patient.build();
}
@凌驾
公共响应更新患者(患者){
Patient currentPatient=patients.get(Patient.getId());
反应;
if(currentPatient!=null){
patients.put(patient.getId(),patient);
response=response.ok().build();
}否则{
抛出新的PatientBusinessException();
}
返回响应;
}
@凌驾
公众响应删除患者(长id){
患者=患者。获取(id);
反应;
如果(患者!=null){
患者。移除(id);
response=response.ok().build();
}否则{
response=response.notModified().build();
}
返回响应;
}
}
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>
<?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>
4.0.0
com.bharath.restws
休息
0.0.1-快照
罐子
休息
病人休息服务
org.springframework.boot
spring启动程序父级
1.5.9.1发布
UTF-8
UTF-8
1.8
org.apache.cxf
cxf弹簧引导启动器jaxrs
3.1.11
org.codehaus.jackson
杰克逊·贾克斯
1.9.13
org.codehaus.jackson
杰克逊xc
1.9.13
org.springframework.boot
弹簧起动试验
测试
公用记录
公用记录
1.2
org.apache.cxf
通用工具
2.0.6
org.springframework.boot
springbootmaven插件
看起来您缺少@RequestBody
注释,该注释指示参数如何绑定到web请求
我不确定您是否正在构建ApacheCfx项目的spring项目。但是,如果您想使用SpringBoot创建服务,这对您的入门非常有用 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.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>
<?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>
4.0.0
com.your.package
病人服务
2.1.1
战争
病人服务
org.springframework.boot
spring启动程序父级
1.5.10.1发布
UTF-8
UTF-8
1.8
真的
5.0.3.发布
2.9.4
5.2.11.最终版本
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
弹簧靴起动器执行器
org.hibernate.validator
休眠验证器
6.0.2.1最终版本
公地io
公地io
2.6
病人服务
org.springframework.boot
springbootmaven插件
看起来您错过了cxf api
依赖项:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.2.3</version>
</dependency>
org.apache.cxf
cxfapi
2.2.3