java—如何消除SpringBoot中bean的重复性能?

rsl1atfo  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(321)

大家好,有个问题,我的豆子被执行了两次,我不明白为什么。我对springboot还不熟悉,恐怕是因为注解使用不当。

package com.Alfa.controllers;

import com.Alfa.tools.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

@SpringBootApplication
@Controller
public class MainController {
    private final RankClient rank;
    private final GifClient gifClient;

    @Autowired
    public MainController(RankClient rank, GifClient gifClient) {
        this.rank = rank;
        this.gifClient = gifClient;
    }

    @GetMapping("/{currency}")
    public String getGif(@PathVariable String currency, Model model) {
        JsonParser jsonParser=new JsonParser();
        System.out.println(currency);
        boolean res = jsonParser.isRankHigherToday(rank.getYesterdayJson(getYesterdayDate(), currency).getBody(), rank.getJson(currency).getBody());
        Map gif;
        if (res == false) gif = gifClient.getBrokeGif().getBody();
        else gif = gifClient.getRichGif().getBody();
        gif = (Map) gif.get("data");
        gif = (Map) gif.get("images");
        gif = (Map) gif.get("downsized_large");
        String GifSrc = (String) gif.get("url");
        model.addAttribute("src", GifSrc);
        return "index";

    }

    private String getYesterdayDate() {
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(cal.getTime());
    }

}

@FeignClient(name = "RankClient", url = "${feign.client.rank.url}")
interface RankClient {
    @RequestMapping(method = RequestMethod.GET, value = "${feign.client.rank.json.latest}"+"${feign.client.rank.token}&base={currency}")
    ResponseEntity<Map> getJson(@PathVariable("currency") String currency);

    @RequestMapping(method = RequestMethod.GET, value = "${feign.client.rank.json.historical}" +
            "{date}" + "${feign.client.rank.json.historical.end}" + "${feign.client.rank.token}&base={currency}")
    ResponseEntity<Map> getYesterdayJson(@PathVariable("date") String date, @PathVariable("currency") String currency);
}

@FeignClient(name = "GifClient", url = "${feign.client.gif.url}")
interface GifClient {
    @RequestMapping(method = RequestMethod.GET, value = "${feign.client.gif.token}" + "${feign.client.gif.tag.broke}")
    ResponseEntity<Map> getBrokeGif();

    @RequestMapping(method = RequestMethod.GET, value = "${feign.client.gif.token}" + "${feign.client.gif.tag.rich}")
    ResponseEntity<Map> getRichGif();
}
``` `System.out.println(currency);` 在我的例子中,返回两个值,第一个是我需要的值,但随后返回favicon。伊科。我不知道favicon.ico来自哪里,我想知道我做错了什么。
pvabu6sv

pvabu6sv1#

web浏览器将请求 /favicon.ico 检索用于在选项卡、书签列表等中表示网站的图标。我怀疑这就是发生在这里的情况。
您可以通过更改uri来避免问题,以便 @GetMapping 与下面的任何东西都不匹配 / . 例如,你可以使用 currencies/{currency} .
或者,如果你想或需要使用 /{currency} ,你可以验证一下 currency 是已知的货币代码,如果不是,则返回404。一些输入验证通常是一个好主意,因此无论方法Map到哪个uri,更改都是值得的。

相关问题