标签: http-request-parameters

无法将自定义验证应用于 requestParam

我有我的 RequestParam,我需要验证它,但是 mu 自定义验证不适用,我的控制器

@RestController
@Validated
class ExchangeController {

    private static final Logger logger = Logger.getLogger(ExchangeController.class.getName());

    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    @Qualifier("dataService")
    private CurrencyExchangeService currencyExchangeService;

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
    public Object converting(@RequestParam("fromCurrency") @NotNull @CurrencyValidation String fromCurrency,
                             @RequestParam("toCurrency") @NotNull String toCurrency,
                             @RequestParam("amount") @NotNull String amount) throws IOException {
        BigDecimal convertedAmount = currencyExchangeService.convert(fromCurrency, toCurrency, new BigDecimal(amount));
        return new ExchangeRateDTO(fromCurrency, toCurrency, new BigDecimal(amount), convertedAmount);

    }
}
Run Code Online (Sandbox Code Playgroud)

和自定义验证

public class ConstractCurrencyValidator implements
            ConstraintValidator<CurrencyValidation, String> {
        @Override
        public void initialize(CurrencyValidation currency) {
        } …
Run Code Online (Sandbox Code Playgroud)

java validation controller http-request-parameters spring-boot

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

Spring启动时的异常处理

我有一个spring-boot应用程序,它具有以下终点:

@RequestMapping("/my-end-point")
public MyCustomObject handleProduct(
  @RequestParam(name = "productId") String productId,
  @RequestParam(name = "maxVersions", defaultValue = "1") int maxVersions,
){
   // my code
}
Run Code Online (Sandbox Code Playgroud)

这应该处理表单的请求

/my-end-point?productId=xyz123&maxVersions=4
Run Code Online (Sandbox Code Playgroud)

但是,当我指定时maxVersions=3.5,这会抛出NumberFormatException(显而易见的原因).我怎样才能优雅地处理这个NumberFormatException并返回错误信息?

java spring exception-handling http-request-parameters spring-boot

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

无法在Spring REST控制器中将Map用作JSON @RequestParam

这个控制器

@GetMapping("temp")
public String temp(@RequestParam(value = "foo") int foo,
                   @RequestParam(value = "bar") Map<String, String> bar) {
    return "Hello";
}
Run Code Online (Sandbox Code Playgroud)

产生以下错误:

{
    "exception": "org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException",
    "message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map': no matching editors or conversion strategy found"
}
Run Code Online (Sandbox Code Playgroud)

我要的是通过一些JSON与bar参数: HTTP://本地主机:8089 / TEMP富= 7&酒吧=%7B%22A%22%3A%22B%22%7D? ,哪里foo7bar{"a":"b"} 为什么春节不能够这个简单的转换吗?请注意,如果将地图用作请求中@RequestBody的一个,它将起作用POST

spring http-request-parameters spring-boot spring-restcontroller

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

Javascript在将其放入历史记录之前更改url参数

我正在使用datatables JQuery插件.我使用它与AJAX和分页(服务器端).每行包含指向该记录详细视图的链接.单击该链接然后在后退按钮上,我想返回上一页.(注意:不能使用数据表自带状态保存).

这可以实现如果您可以在将url参数放入历史记录之前将其添加到当前URL.

可以这样做吗?纯JS或JQuery并不重要.

(注意:我很新做这种东西,已经读过关于使用#的内容,但从未这样做过,所以如果你建议请提供一个适用于我的问题的例子)

编辑:

非常基本的JQuery插件指南bbq(http://benalman.com/projects/jquery-bbq-plugin/)

假设URL的散列为#a = 2&b = test,您可以通过以下方式获取值:

//true = optional converts 2 to an integer, false to boolean,...
var a = $.bbq.getState('a',true);
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式更改/添加值:

var hash = "a=3&b=hello";
$.bbq.pushState(hash);
Run Code Online (Sandbox Code Playgroud)

pushState在绑定时触发hashchange事件.要绑定事件,但在doucment.ready函数中跟随:

// called when # part of URL is modified
$(window).bind( 'hashchange', function( event ) {
    var hash = event.fragment; // full hash as string
    var a = event.getState('a', true ); // value for a
    // here do meaningful action like an AJAX request using the hash …
Run Code Online (Sandbox Code Playgroud)

javascript browser-history http-request-parameters

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