简体   繁体   English

更改 Java 的 HTML 输出

[英]Change the HTML output from Java

I have the following code:我有以下代码:

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

}

The problem is currently the website looks like this:问题是目前网站看起来像这样:

Website_Curr网站_Curr

But I want a new line for every metric not in one row.但我想要一个新的一行,而不是在一行中的每个指标。 As you see I tried to add <\\b> at the string connotation Do you have an idea how to fix this?如您所见,我尝试在字符串含义处添加 <\\b> 您知道如何解决此问题吗? It is my first web application I am a bit stuck.这是我的第一个 Web 应用程序我有点卡住了。

I appreciate every help!我感谢每一个帮助!

Your "<\\b>" breaks it.你的 "<\\b>" 打破了它。 If you remove it and add a newLine "\\n" it should work.如果您删除它并添加一个 newLine "\\n" 它应该可以工作。 Like this:像这样:

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");
}

Also you are returning plain text.你也返回纯文本。 So, to display it correctly add "produces = "text/plain" to return the formatted String.因此,要正确显示它,请添加 "produces = "text/plain" 以返回格式化的字符串。

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

Then your output will be displayed with line breaks.然后您的输出将显示换行符。 This way u can apply further formatting.这样你就可以应用进一步的格式。

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

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