Springmvc cross-domain settings

  crossdomain, spring-mvc, springboot

explain

You can set them one by one using comments, and you can configure global settings using.

CrossOrigin annotation

    @CrossOrigin(origins = "http://localhost:3000")
    @RequestMapping(value="/search",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询")
    public void search(
                                   @ApiParam(value = "分页,从0开始")
                           @RequestParam(required = false,defaultValue = "0") int page,
                                   @ApiParam(value = "分页大小,默认10")
                           @RequestParam(required = false,defaultValue = "10") int size) {
        ...
    }

Global settings

@Configuration
public class ApiAuthConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        registry.addMapping("/**").allowedOrigins("http://localhost:3000");
    }
}

Jquery request

function search() {
                $.ajax({
                    url: 'http://localhost:8082/api/search',
                    type: 'GET',
                    data: {'keyword':'java'},
                    success: function (res, status) {
                        if (status == 'success') {
                            console.log(res.data);
                            }
                    },
                    error: function (data, status) {
                        if (status == "error") {
                            console.log(data);
//                            alert(data);
                        }
                    }
                });

References