javaproperties,spring-boot (9)---STS 新建一个spring-boot rest 项目

 2023-09-26 阅读 26 评论 0

摘要:STS 新建一个spring-boot rest 项目 1.打开STS,右键选择项目 2.下一步选择consuming rest,红色箭头指向,点击finsh javaproperties?3.代码修改 Application.java package hello;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springf

STS 新建一个spring-boot rest 项目

 

1.打开STS,右键选择项目

这里写图片描述

2.下一步选择consuming rest,红色箭头指向,点击finsh 
这里写图片描述

javaproperties?3.代码修改 
Application.java

package hello;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
public class Application implements CommandLineRunner {private static final Logger log = LoggerFactory.getLogger(Application.class);public static void main(String args[]) {SpringApplication.run(Application.class);}@Overridepublic void run(String... strings) throws Exception {RestTemplate restTemplate = new RestTemplate();Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);log.info(quote.toString());}
}

GreetingController.java

package hello;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;/**   
* <p></p>
* @title          - GreetingController.java 
* @author         - ningzhong.zeng
*/
@Controller
@RequestMapping("/hello-world")
public class GreetingController {@ResponseBody@RequestMapping(value="/greeting", method=RequestMethod.GET)public Quote greeting(@RequestParam(value="name", defaultValue="World") String name) {Value value = new Value();value.setId(1111111L);value.setQuote(name);Quote quote = new Quote();quote.setValue(value);quote.setType("type");return quote;}
}

Qutoe.java

package hello;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {private String type;private Value value;public Quote() {}public String getType() {return type;}public void setType(String type) {this.type = type;}public Value getValue() {return value;}public void setValue(Value value) {this.value = value;}@Overridepublic String toString() {return "Quote{" +"type='" + type + '\'' +", value=" + value +'}';}
}

Value.java

package hello;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {private Long id;private String quote;public Value() {}public Long getId() {return this.id;}public String getQuote() {return this.quote;}public void setId(Long id) {this.id = id;}public void setQuote(String quote) {this.quote = quote;}@Overridepublic String toString() {return "Value{" +"id=" + id +", quote='" + quote + '\'' +'}';}
}

application.properties

server.port : 9000
management.port: 9001
management.address: 127.0.0.1

java调用rest接口?pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-consuming-rest</artifactId><version>0.1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-logging-juli</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

4.运行 
这里写图片描述

5.访问 
http://localhost:9000/hello-world/greeting

http://localhost:9001/hello-world/greeting 
http://localhost:9001/health

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/97901.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息