繁体   English   中英

更改 Java 的 HTML 输出

[英]Change the HTML output from Java

我有以下代码:

@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;
    }

}

问题是目前网站看起来像这样:

网站_Curr

但我想要一个新的一行,而不是在一行中的每个指标。 如您所见,我尝试在字符串含义处添加 <\\b> 您知道如何解决此问题吗? 这是我的第一个 Web 应用程序我有点卡住了。

我感谢每一个帮助!

你的 "<\\b>" 打破了它。 如果您删除它并添加一个 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")

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

暂无
暂无

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

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