没有找到 return 类型值的转换器 - Spring Rest API with db call

No converter found for return value of type - Spring Rest API with db call

编辑:我想出了如何解决这个问题并将其发布在下面的答案中。

我正在尝试创建一个查询数据库和 return 两个值的 API。我知道数据库查询有效,因为我可以看到它将值吐出到控制台。但是当我执行它时,出现此错误:找不到 return 类型值的转换器:class hello.GetPatientInfo

这是我的控制器代码:

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PatientInfoController {

    @RequestMapping("/patientinfo")
    @ResponseBody
    public GetPatientInfo getPatientInfo (@RequestParam String ssnum){
        return new GetPatientInfo(ssnum);
    }
}

这是我的class:

package hello;

import com.automation.dataprovider.DatabaseFunctions;

public class GetPatientInfo {
    DatabaseFunctions db = new DatabaseFunctions();
    private final String pname;
    private final String patno;

    public GetPatientInfo(String ssnum){
        this.patno = getPatNo(ssnum);
        this.pname = getPname(ssnum);

    }

    public String getPatNo(String ssnum){
        String query = "SELECT PATNO FROM lib.PATIENTS WHERE SSNUM = '" + ssnum +"'";
        String patno = db.returnSingleValue(query).toString();
        System.out.println(patno);
        return patno;
    }

    public String getPname(String ssnum){
        String query = "SELECT PNAME FROM lib.PATIENTS WHERE SSNUM = '" + ssnum +"'";
        String pname = db.returnSingleValue(query).toString();
        System.out.println(pname);
        return pname;
    }
}

我确定我做的事情根本上是错误的,因为我以前从未创建过 api,但我只是无法快速取得进展。任何帮助将不胜感激。

你可以尝试让你的classGetPatientInfo独立于数据库。你应该总是 return 只有一个包装器 class 用于你的数据库信息。所以包装器 class 根本不应该进行数据库调用。

所以你可以做这样的事情。 包裹你好;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.automation.dataprovider.DatabaseFunctions;

@RestController
public class PatientInfoController {



@RequestMapping("/patientinfo")
@ResponseBody
public GetPatientInfo getPatientInfo (@RequestParam String ssnum){
    DatabaseFunctions db = new DatabaseFunctions();
    String pname = db.returnSingleValue("SELECT PNAME FROM lib.PATIENTS WHERE SSNUM = '" + ssnum +"'";).toString();
    String patno = b.returnSingleValue(SELECT PATNO FROM lib.PATIENTS WHERE SSNUM = '" + ssnum +"'").toString();
    return new GetPatientInfo(pname, patno);
}

然后在 GetPatienInfo

package hello;

import com.automation.dataprovider.DatabaseFunctions;

public class GetPatientInfo {
DatabaseFunctions db = new DatabaseFunctions();
private final String pname;
private final String patno;

public GetPatientInfo(String pname, String patno){
    this.patno = patno;
    this.pname = pname;

}

public String getPatNo(){
    return this.patno;
}

public String getPname(){
    return this.pname;
}
}

就像@Bob Drinks 在他的评论中提到的那样:查看 以确保您添加了 return [=29] 所需的依赖项=].

我发现我做错了什么,我需要传入两个变量,以便它们被设置然后返回。

private final String pname = "";
    private final String patno = "";

    @RequestMapping("/patientinfo")
    @ResponseBody
    public GetPatientInfo getPatientInfo (@RequestParam String ssnum){
        return new GetPatientInfo(pname, patno, ssnum);
    }

这样做更正了收到的错误。