springboot(服务端接口)获取URL请求参数的七种方法

三个常用注解:

  @RequestParam、@RequestBody、 @PathVariable、

1.@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)

 例如:URL:localhost:8080/del?id=3

    @ResponseBody
    @GetMapping("/del")
    public String deleteUserByIdOneContorller(@RequestParam("id") int id){
        boolean resulte = userService.deleteUserByIdOne(id);
        if (resulte){
            return "删除成功";
        }
        return "删除失败!";
    }

2.@PathVariable可以用来映射URL中的占位符到目标方法的参数中

 例如:URL:localhost:8080/findSpecsByPhoneId/3

@GetMapping("/findSpecsByPhoneId/{phoneId}")
    public ResultVO findSpecsByPhoneId(@PathVariable("phoneId") Integer phoneId){
        return ResultVOUtil.success(phoneService.findSpecsByPhoneId(phoneId));

    }

3、通过HttpServletRequest接收,post方式和get方式都可以。

@RequestMapping("/addUser2")
    public String addUser2(HttpServletRequest request) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

4. 用注解@RequestBody绑定请求参数到方法入参  用于POST请求,UserDTO 这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。

@RequestMapping(value="/addUser6",method=RequestMethod.POST)
    public String addUser6(@RequestBody UserDTO userDTO) {
        System.out.println("username is:"+userDTO.getUserName());
        System.out.println("password is:"+userDTO.getPassWord());
        return "demo/index";
    }

5.直接把表单的参数写在Controller相应的方法的形参中,提交的参数需要和Controller方法中的入参名称一致。

例如:URL:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111

 public String addUser1(String username,String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

6.通过一个bean来接收,post方式和get方式都可以。

(1)建立一个和表单中参数对应的bean

public class UserModel {
    
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

(2)用这个bean来封装接收的参数

 @RequestMapping("/addUser3")
    public String addUser3(UserModel user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "demo/index";
    }

7.使用@ModelAttribute 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!您可以使用Spring Boot来实现SOAP(Simple Object Access Protocol)WS(Web Services)的服务端。下面是一个简单的示例代码来演示如何实现: 1. 首先,您需要在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web Services --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <!-- Apache CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.1</version> </dependency> </dependencies> ``` 2. 创建一个SOAP服务接口,例如: ```java import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface MySoapService { @WebMethod String processRequest(@WebParam(name = "param") MyComplexParam param); } ``` 3. 创建一个复杂参数类,例如: ```java public class MyComplexParam { private String name; private int age; // 其他属性和方法 // Getter和Setter方法省略 } ``` 4. 创建一个实现SOAP服务接口的类,例如: ```java import javax.jws.WebService; @WebService(endpointInterface = "com.example.MySoapService") public class MySoapServiceImpl implements MySoapService { @Override public String processRequest(MyComplexParam param) { // 处理请求的逻辑 return "Hello, " + param.getName() + "! Your age is " + param.getAge() + "."; } } ``` 5. 创建一个配置类,用于发布SOAP服务,例如: ```java import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.xml.ws.Endpoint; @Configuration public class WebServiceConfig { @Bean public MySoapService mySoapService() { return new MySoapServiceImpl(); } @Bean(name = Bus.DEFAULT_BUS_ID) @Primary public SpringBus springBus() { return new SpringBus(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), mySoapService()); endpoint.publish("/mySoapService"); // 指定发布的URL路径 return endpoint; } } ``` 6. 运行Spring Boot应用程序,您的SOAP服务将在指定的URL路径上发布。 7. 使用Postman或其他工具来发起SOAP请求。您可以将复杂参数作为XML或JSON进行传输,具体取决于您在请求中使用的Content-Type。 希望这些代码示例能帮助您实现Spring Boot SOAP WS的服务端,并成功接收到复杂参数。如有需要,请根据您的实际情况进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值