问题描述
在 java 中,我尝试使用以下代码访问 Spring Boot 微服务:
In java, I am trying to access a Spring Boot Microservice using the following code:
try {
CircularsSaveParams circularsSaveParams;
circularsSaveParams= new CircularsSaveParams( request, principal, session, locale, mav,souqBean); //creating microservice params
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> entity = new HttpEntity<Object>(circularsSaveParams,headers);
ResponseEntity<Boolean> responseEntity = restTemplate.exchange("http://localhost:8084/circular-save", HttpMethod.POST, entity, Boolean.class);
//String employeeDivision= restTemplate.getForObject("http://employee-division-service/"+id+d, String.class);
status= responseEntity.getBody();
}
catch(Exception e)
{
e.printStackTrace();
}
在这里,CircularsSaveParams
类的字段似乎导致了问题.下面是那个类,
Here, the fields of the class CircularsSaveParams
seems to cause the problem. Below is that class,
import java.security.Principal;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import ae.gov.adm.saeed.bean.CircularsBean;
public class CircularsSaveParams {
HttpServletRequest request;
Principal principal;
HttpSession session;
Locale locale;
ModelAndView mav;
CircularsBean souqBean;
public CircularsSaveParams()
{
}
public CircularsSaveParams(HttpServletRequest request, Principal principal, HttpSession session, Locale locale,
ModelAndView mav, CircularsBean souqBean) {
super();
this.request = request;
this.principal = principal;
this.session = session;
this.locale = locale;
this.mav = mav;
this.souqBean = souqBean;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public Principal getPrincipal() {
return principal;
}
public void setPrincipal(Principal principal) {
this.principal = principal;
}
public HttpSession getSession() {
return session;
}
public void setSession(HttpSession session) {
this.session = session;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public ModelAndView getMav() {
return mav;
}
public void setMav(ModelAndView mav) {
this.mav = mav;
}
public CircularsBean getSouqBean() {
return souqBean;
}
public void setSouqBean(CircularsBean souqBean) {
this.souqBean = souqBean;
}
}
当我运行代码时,在这一行出现以下错误,
When I run the code, I get the following error on this line,
ResponseEntity<Boolean> responseEntity = restTemplate.exchange("http://localhost:8084/circular-save", HttpMethod.POST, entity, Boolean.class);
下面是堆栈跟踪.
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.util.logging.SimpleFormatter and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.util.logging.SimpleFormatter and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: ae.gov.adm.saeed.params.CircularsSaveParams["request"]->org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest["request"]->ae.gov.adm.saeed.web.security.AuthFilterHttpServletRequestWrapper["request"]->org.apache.catalina.connector.RequestFacade["servletContext"]->org.apache.catalina.core.ApplicationContextFacade["classLoader"]->org.apache.catalina.loader.ParallelWebappClassLoader["resources"]->org.apache.catalina.webresources.StandardRoot["context"]->org.apache.catalina.core.StandardContext["logger"]->org.apache.juli.logging.DirectJDKLog["logger"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.RootLogger["handlers"]->java.util.logging.ConsoleHandler["formatter"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106)
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:876)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:658)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
at ae.gov.adm.saeed.web.controller.util.CircularsControllerUtil.saveCircularView(CircularsControllerUtil.java:357)
at ae.gov.adm.saeed.web.controller.CircularsController.saveCircular(CircularsController.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:335)
at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:610)
at ae.gov.adm.common.web.filter.AbstractFilter.doFilter(AbstractFilter.java:47)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.util.logging.SimpleFormatter and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: ae.gov.adm.saeed.params.CircularsSaveParams["request"]->org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest["request"]->ae.gov.adm.saeed.web.security.AuthFilterHttpServletRequestWrapper["request"]->org.apache.catalina.connector.RequestFacade["servletContext"]->org.apache.catalina.core.ApplicationContextFacade["classLoader"]->org.apache.catalina.loader.ParallelWebappClassLoader["resources"]->org.apache.catalina.webresources.StandardRoot["context"]->org.apache.catalina.core.StandardContext["logger"]->org.apache.juli.logging.DirectJDKLog["logger"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.Logger["parent"]->java.util.logging.RootLogger["handlers"]->java.util.logging.ConsoleHandler["formatter"])
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serializeContents(ObjectArraySerializer.java:241)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:201)
at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.serialize(ObjectArraySerializer.java:25)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:851)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286)
请注意,我定义了以下方法:
Please note that I have the below method defined:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
return converter;
}
如何解决问题?
更新:经过一些研究,我尝试像这样定义我的对象:
Update: I have tried to define my object like this, after some research:
circularsSaveParams= mapper.convertValue(new CircularsSaveParams( request, principal, session, locale, mav,souqBean), CircularsSaveParams.class); //creating microservice params
然而,问题并没有解决.出现同样的错误.
However, the problem was not solved. Same error appears.
编辑服务器代码如下所示:
@RequestMapping(value="/circular-save", method=RequestMethod.POST)
public Boolean saveCircularView(@RequestBody CircularsSaveParams circularsSaveParams) {
System.out.println( "circular-save microservice Called..................................");
HttpServletRequest request= circularsSaveParams.getRequest();
Principal principal= circularsSaveParams.getPrincipal();
HttpSession session= circularsSaveParams.getSession();
Locale locale = circularsSaveParams.getLocale();
CircularsBean souqBean = circularsSaveParams.getSouqBean();
boolean status = false;
boolean isModifyRequest = false;
GmCirculars gmCircular = null;
try {
if(null!=souqBean.getCircularEntryId() && !BigDecimal.ZERO.equals(souqBean.getCircularEntryId())) {
gmCircular = circularsService.findById(souqBean.getCircularEntryId());
isModifyRequest = true;
}
gmCircular = souqBean.populateCircularEntity(gmCircular);
ApplicationUser applicationUser = getLoggedInUser(request, principal);
gmCircular.setOrganization(applicationUser.getOrganization());
//LOG.info("Ajax Call to Save Circular [" + gmCircular + "]");
if(!isModifyRequest) {
gmCircular.setCreatedBy(applicationUser);
gmCircular.setCreatedOn(new Date());
}
if(souqBean.getShowOnDashboard() == null)
gmCircular.setShowOnDashboard(new BigDecimal(0));
GmCirculars savedObj = circularsService.saveOrUpdate(gmCircular);
if(null!=savedObj && null != savedObj.getId() && !BigDecimal.ZERO.equals(savedObj.getId())) {
status = true;
}
else {
status = false;
}
String externaDoclId=null;
//Saving Attachment
MultipartFile fileDataIcon = souqBean.getCircularAttachment();
if (null != savedObj && null != savedObj.getId() && !BigDecimal.ZERO.equals(savedObj.getId())) {
if (null != fileDataIcon && fileDataIcon.getSize() > 0) {
Documents uploadedDoc = uploadMuFile(request, fileDataIcon, principal, savedObj.getId(), ModuleEnums.GM_CIRCULARS,null);
if (uploadedDoc != null && uploadedDoc.getId() != null) {
savedObj.setDocumentId(uploadedDoc.getId()); //Modified BY ZAID ZAID ZAID ZAID ZAID ZAID
savedObj = circularsService.saveOrUpdate(gmCircular);
status = true;
externaDoclId=uploadedDoc.getExternalDocumentId();
}
else {
status = false;
}
}
else { // without doc
status = true;
}
if(status) {
saveUserActivityLog(applicationUser, ModuleEnums.GM_CIRCULARS.getValue(), savedObj.getId(),
isModifyRequest ? ActionTypeEnums.EDIT.getValue() : ActionTypeEnums.ADD.getValue());
}
}
}
catch (Exception e) {
status = false;
e.printStackTrace();
}
System.out.println("circulars save microservice is DONE!........................................status is:"+status);
return status;
}
推荐答案
您的想法有误.您不能也不应该在应用程序之间共享请求.您应该使用一些服务用户或一些 oauth 生成的令牌来与其他 Restful 微服务进行通信,而不是将用户主体传递过来.如果你有一种独特的方式来识别用户,比如某个序列号,你总是可以从数据库中拉入用户.
You are thinking it incorrectly. You can't and you shouldn't share requests between applications. You should use some service user or some oauth generated tokens to communicate with other restful micro services as oppose to passing user principal over. You can always pull in the user from db if you have a unique way to identify user like some sequence number.
如果您尝试将后端服务移动到另一个微服务,您应该只发送后端所需的信息,并让调用微服务处理身份验证/授权和用户会话/审计.
If you trying to move your backend services to another microservices you should only send the information that is needed for backend and have the calling microservice take care of authentication/authorization and user sessions/auditing.
从您的服务器实现来看,您似乎正在跟踪向微服务发出的请求.这可以在发送请求之前调用微服务进行跟踪.如果你有很多这样的微服务,你可以创建一个中央代理(另一个微服务),在路由到被调用的微服务之前负责记录/审计用户请求.
From your server implementation it looks like you are tracking who/what request is made to the microservice. This can be tracked in calling microservices before you sent the request. If you many such microservices you could create a central proxy (another microservice) which takes care of logging/auditing user requests before routing to called microservice.
这篇关于如何修复错误 No serializer found for class java.util.logging.SimpleFormatter and no properties found to create BeanSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!