2016-05-11 5 views
-1

私はspring-bootとwebserviceの初心者です。私は2つの演習を行い、クライアントとrun()メソッドの実装方法を知っています。文字列トラフを送受信するこのWebサービスでは?RestfullとSpringについての情報を送信して取得する

PROJECT CONSUMER

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

PROJECTプロデューサー

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

PROJECTプロデューサー

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

} 

プロジェクトPRODUCER

import org.springframework.stereotype.Component; 

@Component 
public class Client { 

    public boolean sendAMessage(String greeting) { 
     // Send the message 

    return false; 
    } 
} 

方法run()sendAMessage()を実装するための任意のヒント?

答えて

0

Spring Bootを使用しており、Webサービスを実装したい場合は、RESTapiを使用してWebサービスを実装するのが最も簡単です。

@ResponseBodyまたは@RestController注釈を使用してサービスを公開できます。

+0

runとsendAmessageメソッドの実装と同じですか? 私はグーグルではありますが、私の場合には当てはまらないと理解しました 目的は単に文字列を送受信することです – Saitama

関連する問題