Tag: spring boot

Multipart文件上传Spring Boot

我使用Spring Boot并希望使用Controller来接收多部分文件上传。 发送文件时,我不断收到错误415不支持的内容类型响应,并且永远不会到达控制器 There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘multipart/form-data;boundary=—-WebKitFormBoundary1KvzQ1rt2V1BBbb8’ not supported 我尝试使用form:action在html / jsp页面中以及在使用RestTemplate的独立客户端应用程序中发送。 所有尝试都给出了相同的结果 multipart/form-data;boundary=XXXXX not supported. 从多部分文档看来,边界参数必须添加到分段上传,但这似乎与接收”multipart/form-data”的控制器不匹配 我的控制器方法设置如下 @RequestMapping(value = “/things”, method = RequestMethod.POST, consumes = “multipart/form-data” , produces = { “application/json”, “application/xml” }) public ResponseEntity submitThing(HttpServletRequest request, @PathVariable(“domain”) String domainParam, @RequestParam(value = “type”) String thingTypeParam, @RequestBody […]

Spring安全CORSfilter

我们在现有项目中添加了Spring Security 。 从这一刻起,我们得到一个401 No ‘Access-Control-Allow-Origin’ header is present on the requested resource我们服务器No ‘Access-Control-Allow-Origin’ header is present on the requested resource错误上。 那是因为没有Access-Control-Allow-Origin标头附加到响应。 为了解决这个问题,我们在注销过滤Filter之前添加了我们自己的filter,它在Filter链中,但filter不适用于我们的请求。 我们的错误: XMLHttpRequest无法加载http://localhost:8080/getKunden 。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 原因http://localhost:3000因此不允许访问。 响应具有HTTP状态代码401。 我们的安全配置: @EnableWebSecurity @Configuration @ComponentScan(“com.company.praktikant”) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private MyFilter filter; @Override public void configure(HttpSecurity http) throws Exception { final UrlBasedCorsConfigurationSource source = […]

除了localhost之外,如何配置与Spring集成的嵌入式Tomcat来监听对IP地址的请求?

我试图从spring指南运行示例: 构建RESTful Web服务 。 如果我打开localhost:8080 / greeting,它会很好用。 但是,如果我打开192.168.1.111:8080/greeting或140.112.134.22:8080/greeting ,它就无法建立连接,尽管我的计算机实际上在互联网上使用了这两个IP。 有人可以建议我如何在Spring中配置嵌入式Tomcat以接受其他IP地址上的HTTP请求,除了localhost(即127.0.0.1)? 谢谢! 🙂

Firebase云消息传递API弹簧

我必须在Spring Java中创建一个Rest API用于多层arch,其中需要为Firebase云消息传递(FCM)构建DAO,Controller,Service Manager以向Android应用程序发送推送通知消息,但我无法配置Java中的服务器,用于向设备发送通知。 我怎么能?

在Spring Boot中返回JSON对象作为响应

我在Spring启动时有一个示例Rest Controller: @RestController @RequestMapping(“/api”) class MyRestController { @GetMapping(path = “/hello”) public JSONObject sayHello() { return new JSONObject(“{‘aa’:’bb’}”); } } 我正在使用json库:org.json 当我点击api /你好时,我得到一个exception说: servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了exception[请求处理失败; 嵌套exception是java.lang.IllegalArgumentException:没有为根本原因找到类型为:class org.json.JSONObject的返回值的转换器 java.lang.IllegalArgumentException:找不到类型为:class org.json.JSONObject的返回值的转换器 问题是什么 有人可以解释究竟发生了什么。 我是SpringBoot的新手。 提前致谢 :)

在spring-boot中过滤顺序

如何在spring-boot中指定filter的顺序? 我需要在Spring Securityfilter之后插入我的MDCfilter。 我几乎尝试了所有东西,但我的滤镜始终是第一个 这不起作用: @Bean @Order(Ordered.LOWEST_PRECEDENCE) public UserInsertingMdcFilter userInsertingMdcFilter() { return new UserInsertingMdcFilter(); } 这也不起作用: @Bean public FilterRegistrationBean userInsertingMdcFilterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); UserInsertingMdcFilter userFilter = new UserInsertingMdcFilter(); registrationBean.setFilter(userFilter); registrationBean.setOrder(Integer.MAX_VALUE); return registrationBean; }

在将应用程序迁移到Spring Boot之后使用Spring Data Rest时,我发现使用@Id的实体属性不再编组为JSON

这个问题与这个SO问题有关( Spring引导@ResponseBody没有序列化实体id )。 我观察到,在将应用程序迁移到Spring Boot并使用spring-boot-starter-data-rest依赖项后,我的实体@Id字段不再在生成的JSON中进行编组。 这是我的请求映射,在调试时,我可以看到数据在返回之前没有被更改,因此@Id属性稍后被剥离。 @RequestMapping(method = RequestMethod.GET, produces = {“application/json”}) public PagedResources receipts(Pageable pageable, PagedResourcesAssembler assembler) { Page receipts = receiptRepository.findByStorerAndCreatedDateGreaterThanEqual(“003845”, createdStartDate, pageable); PagedResources pagedResources = assembler.toResource(receipts, receiptResourceAssembler); return pagedResources; } 是否有一个设置允许我在生成的JSON中保留@Id字段,因为我的应用程序允许用户按该值进行搜索。 谢谢 :)

Spring Boot REST API – 请求超时?

我有一个Spring Boot REST服务,有时会将第三方服务作为请求的一部分。 我想在我的所有资源上设置一个超时(让我们说5秒),这样如果任何请求处理(整个链,从传入到响应)花费的时间超过5秒,我的控制器会响应HTTP 503而不是实际响应。 如果这只是一个Spring属性,例如设置,那将是非常棒的 spring.mvc.async.request-timeout=5000 但我没有运气。 我也尝试过扩展WebMvcConfigurationSupport并覆盖configureAsyncSupport: @Override public void configureAsyncSupport(final AsyncSupportConfigurer configurer) { configurer.setDefaultTimeout(5000); configurer.registerCallableInterceptors(timeoutInterceptor()); } @Bean public TimeoutCallableProcessingInterceptor timeoutInterceptor() { return new TimeoutCallableProcessingInterceptor(); } 没有运气。 我怀疑我必须手动计算所有第三方电话,如果他们花了太长时间,则抛出超时exception。 是对的吗? 或者是否有涵盖我所有请求端点的更简单,整体的解决方案?

如何找到Spring Boot中当前登录的用户?

在这个Spring Boot应用程序中有一个Web服务,它为登录用户返回一些数据: @RequestMapping(“/resource”) public Map home() { Map model = new HashMap(); model.put(“id”, UUID.randomUUID().toString()); model.put(“content”, “Hello World”); return model; } 想象一下,该方法的返回值取决于当前登录的用户。 如何找出该方法中登录的用户?

使用Spring Boot和annotations配置ViewResolver为HTTP请求找不到URI错误的映射

我正在尝试使用gradle,spring boot和spring mvc以最简单的视图解析器和html制作“hello world”应用程序。 我从thymeleaf spring boot示例开始 ,我只是想删除thymeleaf,使用纯html和InternalResourceViewResolver创建一个更简单的mvc应用程序。 我有一个greeting.html我想要服务,它位于src / main / webapp / WEB-INF。 当我运行应用程序时,我得到了 No mapping found for HTTP request with URI [/WEB-INF/greeting.html] in DispatcherServlet with name ‘dispatcherServlet’ 这是一个常见的错误,网上有很多答案,但似乎没有任何帮助。 这是我的Application.java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 这是我的GreetingController.java @Controller public class GreetingController { @RequestMapping(“/greeting”) public […]