小编The*_*heo的帖子

如何选择Spring Boot webapp的URL?

我使用Spring Boot创建一个Web应用程序,我不知道如何将URL更改localhost:8080为类似的东西localhost:8080/myWebApp.

我看到很多资源在线引用application.properties文件并将其添加到类路径中.但是,我不确定究竟放在哪里.

问题

  • 在我的src/main/resources

  • 我如何在文件中分配URL?

url web-applications spring-boot

47
推荐指数
4
解决办法
8万
查看次数

使用Stomp将多条消息发送到同一主题

所以我正试图从服务器向我的浏览器发送消息,并有两个方法应该被连线以将消息发送到STOMP主题.

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public static Greeting greeting(HelloMessage message) throws Exception {
    System.out.println("Sending message...");
    Application.startBody(message.getName());
    return new Greeting("Hello, " + message.getName() + "!");
}

@SendTo("/topic/greetings")
public static  Greeting security(String message) {
    System.out.println("entered the informer");
    return new Greeting("bye, " + message + "!");
}
Run Code Online (Sandbox Code Playgroud)

第一个也映射为接收消息并发回一个消息.第一个功能工作,消息将其返回到浏览器并显示在网页上.然而,第二次不起作用.它永远不会在网页的控制台中显示收到的消息.我只能用一种方法发送到同一主题吗?我尝试更改主题并向我的Stomp客户端添加订阅,但这也无效.它与第二种静态方法有什么关系吗?(我需要从一个单独的类中调用它.)

这是我在html文件中的订阅:

    function connect() {
        var socket = new SockJS("http://localhost:8080/hello");
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是我的WebSocketConfig.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; …
Run Code Online (Sandbox Code Playgroud)

spring annotations stomp websocket

5
推荐指数
1
解决办法
6276
查看次数

Spring STOMP从应用程序的任何位置发送消息

我正在构建一个使用Stomp通过websockets代理消息的应用程序.我试图从服务器向客户端发送消息,而无需来自应用程序的任何位置的请求.我在网上找到两个单独的选择,用于从应用程序的任何位置发送消息

第一个是在Websocket文档中找到的.第20.4.5节:

@Controller
public class GreetingController {

    private SimpMessagingTemplate template;

    @Autowired
    public GreetingController(SimpMessagingTemplate template) {
        this.template = template;
    }

    @RequestMapping(value="/greetings", method=POST)
    public void greet(String greeting) {
        String text = "[" + getTimestamp() + "]:" + greeting;
        this.template.convertAndSend("/topic/greetings", text);
    }

}
Run Code Online (Sandbox Code Playgroud)

第二个是由一位Spring Blogger撰写的指南:

@Controller
public class GreetingController {

  @Autowired
  private SimpMessagingTemplate template;


  @RequestMapping(value="/greeting", method=POST)
  public void greet(String greeting) {
    String text = "[" + getTimeStamp() + "]:" + greeting;
    this.template.convertAndSend("/topic/greeting", text);
  }

}
Run Code Online (Sandbox Code Playgroud)

两者非常相似.第一个覆盖默认构造函数,不会自动装配模板初始化.第二个不会创建新的构造函数,但会自动装配模板初始化.我的第一个问题是这两个行为是否相同?

更紧迫的是我无法从任何地方调用"问候"方法.我尝试过几种不同的方式. …

java spring annotations stomp autowired

5
推荐指数
1
解决办法
5664
查看次数

在@Autowire调用期间未实例化服务类

我试图在我的消息传递应用程序中使用@Service类,但是当我从泛型类中尝试时,该类不会通过@Autowire实例化.它只在我使用Controller时实例化.

这是我的控制器:

    package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import hello.Application;

@Controller
public class HelloController {


    @Autowired
    private MessageSender sender;

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        System.out.println("Sending message...");
        beginRoute(message.getName());
        sender.greet("thunder");
        return new Greeting("Hello, " + message.getName() + "!");
    }

    public void beginRoute(String message) {
        Application.startBody(message);
    }
}
Run Code Online (Sandbox Code Playgroud)

上述对sender.greet的调用是成功的.

这是我尝试使用该服务的另一个类:

package com.routing.integration;

import hello.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations; …
Run Code Online (Sandbox Code Playgroud)

java spring stomp autowired spring-boot

1
推荐指数
1
解决办法
2921
查看次数