【问题标题】:Change the HTML output from Java更改 Java 的 HTML 输出
【发布时间】:2020-06-29 13:43:19
【问题描述】:

我有以下代码:

@Controller
public class GatesController {

    @RequestMapping ("/gates")

    public static String qualityGates(String x) throws IOException {
        try {
            System.out.println("\n------QualityGates------");
            URL toConnect = new URL(x);
            HttpURLConnection con = (HttpURLConnection) toConnect.openConnection();
            System.out.println("Sending 'GET' request to URL : " + x);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();

            //Cast the JSON-File to a JSONObject
            JSONObject res = new JSONObject(response.toString());
            JSONArray gates = new JSONArray(res.getJSONObject("projectStatus").getJSONArray("conditions").toString());
            JSONObject test = new JSONObject(res.getJSONObject("projectStatus").toString());

            String a = ("\nThe current Project-Status is: " + test.get("status") + "\n");
            String b = "";
            for (int i = 0; i < gates.length(); i++) {
                String status = gates.getJSONObject(i).getString("status");
                String metric = gates.getJSONObject(i).getString("metricKey");
                b = b + ("<\b>Status: " + status + " | Metric: " + metric);

            }

            System.out.println(a+b);
            return a + b;
        } catch (Exception e) {
            System.out.println(e);
            return String.format("Error");
        }
    }

@SpringBootApplication
@RestController
public class SonarQualityGatesApplication {

    public static void main(String[] args) throws IOException {
        ConfigurableApplicationContext context=SpringApplication.run(SonarQualityGatesApplication.class, args);
        TestController b = context.getBean(TestController.class);


    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

    @GetMapping("/gates")
    public String gates() throws IOException {
        String temp = qualityGates("http://localhost:9000/api/qualitygates/project_status?projectKey={PROJECT_KEY}");
        return temp;
    }

}

问题是目前的网站是这样的:

Website_Curr

但我希望每个指标都有一个新行,而不是一行。 如您所见,我尝试在字符串内涵处添加 你知道如何解决这个问题吗?这是我的第一个 Web 应用程序,我有点卡住了。

感谢每一个帮助!

【问题讨论】:

  • 为什么不用MVC?
  • @kulsin 打扰一下,MVC?

标签: java html spring spring-boot web


【解决方案1】:

你的 "" 打破了它。如果你删除它并添加一个 newLine "\n" 它应该可以工作。 像这样:

String a = ("\nThe current Project-Status is: " + test.get("status") + "\n");
String b = "";
for (int i = 0; i < gates.length(); i++) {
   status = gates.getJSONObject(i).getString("status");
   String metric = gates.getJSONObject(i).getString("metricKey");
   b = b + ("Status: " + status + " | Metric: " + metric + "\n");
}

您还返回纯文本。因此,要正确显示它,请添加“produces = “text/plain”以返回格式化的字符串。

@GetMapping(value = "/gates", produces = "text/plain")

然后您的输出将显示换行符。这样你就可以应用进一步的格式。

【讨论】:

  • 首先谢谢!我只是添加了
    而不是 并且效果很好!
  • 太棒了。很高兴听到。自己应该看到这个,但很着急。如果它解决了您的问题,请接受我的说法。
猜你喜欢
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多