我是spring-boot和webservice的初学者,我有两个练习要做,他们会知道如何实现客户端和run()方法;发送和接收字符串在这个网络服务?
项目消费者
public class Service implements Runnable {
public static void main(String args[]) {
System.out.println("Send the messages....");
Thread thread = new Thread(new Service());
thread.start();
}
public void run() {
// Loop receiving messages
}
}
项目生产者
@Path("/greet")
@Component
public class GreetResource {
static Logger logger = LoggerFactory.getLogger(GreetResource.class);
@Autowired
Client client;
public GreetResource() {
logger.info("Resource Initialized");
}
@GET
@Path("/echo/{msg}")
@Produces({ MediaType.TEXT_PLAIN_VALUE })
public Response echo(@PathParam("msg") String message) {
return Response.ok().entity(message).build();
}
@POST
@Path("/send")
@Consumes({ MediaType.TEXT_PLAIN_VALUE })
public boolean sendMessage(String greeting) {
client.sendAMessage(greeting);
return true;
}
}
项目生产者
@EnableAutoConfiguration
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
@Autowired
private Environment env;
@PostConstruct
public void initApplication() throws IOException {
if (env.getActiveProfiles().length == 0) {
logger.warn("No Spring profile configured, running with default configuration");
} else {
logger.info("Running with Spring profile(s) : {}", Arrays.toString(env.getActiveProfiles()));
Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
if (activeProfiles.contains("dev") && activeProfiles.contains("prod")) {
logger.error("You have misconfigured your application! "
+ "It should not run with both the 'dev' and 'prod' profiles at the same time.");
}
}
}
public static void main(String[] args) {
logger.info("weather application service starting...");
SpringApplication.run(Application.class, args);
}
}
项目生产者
import org.springframework.stereotype.Component;
@Component
public class Client {
public boolean sendAMessage(String greeting) {
// Send the message
return false;
}
}
有关实施方法run ()
和sendAMessage()
的任何提示吗?
答案 0 :(得分:0)
如果您正在使用Spring Boot并希望实现Web服务,那么最简单的方法是使用RESTapi实现Web服务。
您可以使用@ResponseBody
或@RestController
注释来公开服务。