Skip to content

Commit 1630c0b

Browse files
committed
add springmvc demo
1 parent 38cbedf commit 1630c0b

File tree

7 files changed

+99
-26
lines changed

7 files changed

+99
-26
lines changed

springboot-example/src/main/java/com/codingapi/springboot/example/controller/OpenController.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

springboot-example/src/main/java/com/codingapi/springboot/example/domain/Demo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Demo {
2525

2626
@Id
2727
@GeneratedValue(strategy = GenerationType.IDENTITY)
28-
private int id;
28+
private Integer id;
2929

3030
private String name;
3131

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codingapi.springboot.example.mapping;
2+
3+
import com.codingapi.springboot.example.domain.Demo;
4+
import com.codingapi.springboot.example.repository.DemoRepository;
5+
import lombok.AllArgsConstructor;
6+
import org.springframework.data.domain.Example;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
11+
import java.util.List;
12+
13+
@Component
14+
@AllArgsConstructor
15+
public class DemoApi {
16+
17+
private final DemoRepository demoRepository;
18+
19+
20+
@ResponseBody
21+
public List<Demo> findByName(@RequestParam("name") String name){
22+
Demo demo = new Demo();
23+
demo.setName(name);
24+
Example.of(demo);
25+
return demoRepository.findAll();
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codingapi.springboot.example.mapping;
2+
3+
import lombok.AllArgsConstructor;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
8+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
9+
import org.springframework.web.util.pattern.PathPatternParser;
10+
11+
import java.lang.reflect.Method;
12+
13+
@Component
14+
@AllArgsConstructor
15+
public class EndpointMapping {
16+
17+
private final RequestMappingHandlerMapping handlerMapping;
18+
19+
public void addGetMapping(String url, Object handler, Method method){
20+
21+
RequestMappingInfo.BuilderConfiguration options = new RequestMappingInfo.BuilderConfiguration();
22+
options.setPatternParser(new PathPatternParser());
23+
24+
RequestMappingInfo mappingInfo = RequestMappingInfo
25+
.paths(url)
26+
.methods(RequestMethod.GET)
27+
.produces(MediaType.APPLICATION_JSON_VALUE)
28+
.options(options)
29+
.build();
30+
31+
handlerMapping.registerMapping(mappingInfo,handler,method);
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codingapi.springboot.example.mapping;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class OpenApiConfiguration {
8+
9+
public static class BeanInit{
10+
11+
public BeanInit(EndpointMapping endpointMapping, DemoApi demoApi) {
12+
try {
13+
endpointMapping.addGetMapping("/open/demo/findByName",demoApi,DemoApi.class.getMethod("findByName", String.class));
14+
} catch (NoSuchMethodException e) {
15+
throw new RuntimeException(e);
16+
}
17+
}
18+
}
19+
20+
@Bean
21+
public BeanInit beanInit(EndpointMapping endpointMapping, DemoApi demoApi){
22+
return new BeanInit(endpointMapping, demoApi);
23+
}
24+
25+
}

springboot-example/src/main/java/com/codingapi/springboot/example/repository/DemoRepository.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.codingapi.springboot.example.repository;
22

33
import com.codingapi.springboot.example.domain.Demo;
4+
import org.springframework.data.domain.Example;
45
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
import java.util.List;
510

611
/**
712
* @author lorne
@@ -10,4 +15,11 @@
1015
public interface DemoRepository extends JpaRepository<Demo,Integer> {
1116

1217

18+
@ResponseBody
19+
default List<Demo> test(@RequestParam("name") String name){
20+
Demo demo = new Demo();
21+
demo.setName(name);
22+
return findAll(Example.of(demo));
23+
}
24+
1325
}

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/handler/ServletExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ServletExceptionHandler implements HandlerExceptionResolver {
1616

1717
@Override
1818
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
19-
log.warn("controller exception:{}",ex.getLocalizedMessage());
19+
log.warn("controller exception:{}",ex.getLocalizedMessage(),ex);
2020

2121
MappingJackson2JsonView view = new MappingJackson2JsonView();
2222
ModelAndView mv = new ModelAndView(view);

0 commit comments

Comments
 (0)