文章目录
cubemall-product
Blog.java
java
package com.xd.cubemall.product.entity;
import lombok.Data;
@Data
public class Blog {
private Long id;
private String title;
private String content;
private String comment;
private String mobile;
}
SearchFeignTest.java
java
package com.xd.cubemall.feign;
import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.product.CubemallProductApplication;
import com.xd.cubemall.product.entity.Blog;
import com.xd.cubemall.product.feign.SearchFeign;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallProductApplication.class)
public class SearchFeignTest {
@Autowired
private SearchFeign searchFeign;
@Test
public void testSayHello() {
R r = searchFeign.sayHello("tom");
System.out.println(r);
}
@Test
public void testSayHello2() {
R r = searchFeign.sayHello2("张三");
System.out.println(r);
}
@Test
public void testGetBlog() {
Blog blog = new Blog();
blog.setId(1l);
blog.setTitle("hello");
blog.setContent("world");
R r = searchFeign.getBlog(blog);
System.out.println(r);
}
@Test
public void testGetBlog2() {
Blog blog = new Blog();
blog.setId(1l);
blog.setTitle("hello");
blog.setContent("world");
R r = searchFeign.getBlog2(blog,"aaa");
System.out.println(r);
}
}
SearchFeign.java
java
package com.xd.cubemall.product.feign;
import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.product.entity.Blog;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
@FeignClient("cubemall-search")
public interface SearchFeign {
@GetMapping("hello/{name}")
R sayHello(@PathVariable("name") String name);
@GetMapping("hello")
R sayHello2(@RequestParam("name") String name);
@PostMapping("/blog")
public R getBlog(@RequestBody Blog blog);
@PostMapping("/blog2")
public R getBlog2(@RequestBody Blog blog, @RequestParam("name") String name);
@GetMapping("/spuinfo/putonsale/{spuId}")
R putOnSale(@PathVariable("spuId") Long spuId);
}
pom.xml
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
yml
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
ip: 127.0.0.1
application:
name: cubemall-product
cubemall-search
HelloController.java
java
package com.xd.cubemall.search.controller;
import com.xd.cubemall.common.utils.R;
import com.xd.cubemall.search.model.Blog;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
@GetMapping("hello/{name}")
public R sayHello(@PathVariable String name) {
return R.ok("hello" + name);
}
//http://localhost:8082/hello?name=lisi
@GetMapping("hello")
public R sayHello2(String name) {
return R.ok("hello" + name);
}
@PostMapping("/blog")
public R getBlog(@RequestBody Blog blog) {
blog.setComment("接收到blog对象");
blog.setMobile("111111");
return R.ok(blog);
}
@PostMapping("/blog2")
public R getBlog2(@RequestBody Blog blog, String name) {
blog.setComment(name);
blog.setMobile("222222");
return R.ok(blog);
}
}