简体   繁体   English

Springboot REST API 不通过简单的 GET 请求返回 JSON

[英]Springboot REST API not returning JSON with simple GET request

I am new to Java and Springboot.我是 Java 和 Springboot 的新手。 Attempting to implement a simple get request with one parameter, terminalId which is a string.尝试使用一个参数 terminalId 实现一个简单的 get 请求,它是一个字符串。

The Findall() endpoint works fine, but when implementing an endpoint to match with strings I run into trouble. Findall() 端点工作正常,但是在实现端点以匹配字符串时遇到了麻烦。

Controller控制器

@RestController
@RequestMapping("/api/private/v1")
public class TransactionController {

    private TransactionService<TransactionResponse> transactionService;

    public TransactionController(TransactionyService<TransactionResponse> transactionService) {
        this.transactionService = transactionService;
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @GetMapping(value = "/transactions")
    public List<TransactionResponse> retrieveTransactions(){
        return transactionService.findAll();
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @GetMapping(value = "transactions/{terminalId}")
    public List<TransactionResponse> retrieveByTerminalId(
            @PathVariable String terminalId) {
        if (terminalId != null) {
            return transactionService.findByTerminalId((terminalId));
//            @ResponseStatus(HttpStatus.OK)
        } else {
            return null;
        }

    }
}

Service服务


    List<T> findAll();

    List<T> findByTerminalId(String terminalId);

    T save(T object);
}

Service服务

@Service
public class TransactionServiceImpl implements TransactionService<TransactionResponse> {

    protected List<TransactionResponse> transactionResponseList = new ArrayList<>();

    @Override
    public List<TransactionResponse> findAll() {
        return transactionResponseList;
    }

    @Override
    public List<TransactioResponse> findByTerminalId(String terminalId) {
        return transactionResponseList;
    }

    @Override
    public TransactionResponse save(TransactionResponse object) {

        if(null != object){
            transactionResponseList.add(object);
        }
        return object;
    }
}

Bootstrap Component引导组件

@Component
public class TransactionBootstrap implements CommandLineRunner {

    private TransactionService<TransactionResponse> transactionService;

    public TransactionBootstrap(TransactionService<TransactionResponse> transactionService) {
        this.transactionService = transactionService;
    }

    @Override
    public void run(String... args) throws Exception {
        loadData();
    }

    private void loadData() {

        TransactionResponse response1 = new TransactionResponse("111", LocalDateTime.now(), LocalDateTime.now());

        TransactionResponse response2 = new TransactionResponse("222", LocalDateTime.now(), LocalDateTime.now(),);



        transactionService.save(response1);
        transactionService.save(response2);

    }
}

I am using ths "localhost:8080/api/private/v1/transactions/111" URL我正在使用“localhost:8080/api/private/v1/transactions/111” URL

I get a whitelabel error page.我得到一个白标错误页面。 Is this a JPA issue?这是一个JPA问题吗? Is this a @RequestParams vs @PathVariable issue?这是@RequestParams 与@PathVariable 的问题吗?

I am totally lost.我完全迷路了。

You have missed / in @GetMapping(value = "transactions/{terminalId}")你错过了 / 在 @GetMapping(value = "transactions/{terminalId}")

Write @GetMapping(value = "/transactions/{terminalId}")写@GetMapping(value = "/transactions/{terminalId}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM