我正在做一个测试REST的Spring Boot项目。
REST服务添加,删除,更新并从内存映射中获取患者详细信息。

虽然我可以使用http://localhost:8443/restws/patientservice/patients(GET)这样的通用URL来获取,添加和删除患者

以下路径参数URL不起作用
http://localhost:8443/restws/patientservice/patients/ {123}

以下是我已实现的pom.xml,接口和服务代码

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 https://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>1.5.9.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ranjan.jaxrs.service</groupId>
<artifactId>PatientService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PatientService</name>
<description>Rest Services for patients</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxrs -->

    <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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

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




PatientService界面

package com.ranjan.jaxrs.service;

import java.util.List;

import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;


@Produces("application/xml,application/json")
@Path("/patientservice")
public interface PatientService {
@Path("/patients")
@GET
public List<Patient> getPatientsList();

@Path("/patients/{patientId}")
@GET
public Patient getPatientById(@PathParam("patientId") long id);

@Path("/patients")
@POST
@Consumes("application/xml,application/json")
public Response createPatient(Patient patient);

@Path("/patients")
@PUT
@Consumes("application/xml,application/json")
public Response updatePatient(Patient patient);

@Path("/patients/{patientId}")
@DELETE
public Response deletePatient(@PathParam(value="patientId") long id);

@Path("/patients")
@DELETE
public Response deletePatient();


}

PatientServiceImpl

 package com.ranjan.jaxrs.service;

  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;

  import javax.ws.rs.NotFoundException;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.WebApplicationException;
  import javax.ws.rs.core.Response;

  import org.springframework.stereotype.Service;

  @Service
public class PatientServiceImpl implements PatientService {

Map<Long,Patient> patientRepository=new HashMap<Long,Patient>();

public  PatientServiceImpl() {

    System.out.println("Inside PAtient service.....");
    init();

}

public void init() {
    Patient patient=new Patient();
    patient.setId(123L);
    patient.setName("Ranjan");

    patientRepository.put(patient.getId(), patient);

}

@Override
public List<Patient> getPatientsList() {
    Collection<Patient> patientCollection=patientRepository.values();
    List<Patient> responseList=new ArrayList<Patient>(patientCollection);
    return responseList;
}

@Override
public Patient getPatientById(long id) {
     Patient patient=patientRepository.get(id);
     if(patient==null) {
         //throw new WebApplicationException(Response.Status.NOT_FOUND);
         throw new NotFoundException();
     }
     return patient;
}

@Override
public Response createPatient(Patient patient) {
    patientRepository.put(patient.getId(), patient);
    return Response.ok().build();
}

@Override
public Response updatePatient(Patient patient) {

    Response response;

    if (patient!=null) {
    patientRepository.put(patient.getId(), patient);
    response=Response.ok().build();
    }else {
        //response=Response.notModified().build();
        throw new PatientBusinessException();
    }

    return response;
}

@Override
public Response deletePatient(long id) {
    patientRepository.remove(id);
    return Response.ok().build();
}

@Override
public Response deletePatient() {
    patientRepository.clear();
    return Response.ok().build();
}


}

收到Stacktrace异常

20-05-18 09:27:18.629  WARN 24876 --- [io-8443-exec-10] o.a.c.j.i.WebApplicationExceptionMapper  : javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.apache.cxf.jaxrs.utils.SpecExceptions.toInternalServerErrorException(SpecExceptions.java:79)
at org.apache.cxf.jaxrs.utils.ExceptionUtils.toInternalServerErrorException(ExceptionUtils.java:113)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.convertExceptionToResponseIfPossible(JAXRSInInterceptor.java:227)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:215)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:262)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:299)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:223)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:274)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.EOFException: No content to map to Object due to end of input
  at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
  at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2691)
  at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
  at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
  at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1348)
  at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1299)
  at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:825)
  at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:789)

最佳答案

这似乎是因为您试图在构造函数中调用init()方法,但是存储库还没有机会被注入。

尝试从构造函数中删除方法调用,并向其添加@PostContruct批注。

10-04 13:12