当前位置: 首页 > 知识库问答 >
问题:

如何在spring boot中重用定制响应

柴飞扬
2023-03-14
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
    noticeService.createNotice(noticeDto);


    ObjectNode jsonResponse = null;
    try {
        ObjectMapper mapper = new ObjectMapper();

        jsonResponse = mapper.createObjectNode();
        jsonResponse.put("code", 200);
        jsonResponse.put("success", true);
        jsonResponse.put("msg", "OK");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    PrintWriter out = response.getWriter();
    response.setStatus(200);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    out.flush();

}
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
    noticeService.createNotice(noticeDto);
    
    return successResponse something...
}

共有1个答案

伍宝
2023-03-14

您可以通过以下方式更改controller方法:

您可以通过多种方式实现这一点。其中一种方式如下所述。

package com.example.demo.controller;

import com.example.demo.dto.NoticeDto;
import com.example.demo.service.NoticeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class DemoController {

    @Autowired(required = false)
    private NoticeService noticeService;

    @PostMapping(value = "/notice", produces = "application/json", 
   consumes="application/json")
public ResponseEntity<Map> createNotice(@RequestBody NoticeDto noticeDto) { //
    noticeService.createNotice(noticeDto);

    ObjectNode jsonResponse = null;
    try {
        ObjectMapper mapper = new ObjectMapper();

        jsonResponse = mapper.createObjectNode();
        jsonResponse.put("code", 200);
        jsonResponse.put("success", true);
        jsonResponse.put("msg", "OK");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new SuccessResponseWrapper<String>(jsonResponse.toPrettyString()).build();

}

}

package com.example.demo.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

public class SuccessResponseWrapper<T> {

private ResponseEntity<T> responseEntity;

public SuccessResponseWrapper(T body) {
    HttpStatus status = HttpStatus.OK;
    responseEntity = new ResponseEntity<>(body, status);
}

public SuccessResponseWrapper(MultiValueMap<String, String> headers) {
    HttpStatus status = HttpStatus.OK;
    responseEntity = new ResponseEntity<>(headers, status);
}

public SuccessResponseWrapper(T body, MultiValueMap<String, String> headers) {
    HttpStatus status = HttpStatus.OK;
    responseEntity = new ResponseEntity<>(body, headers, status);
}

public SuccessResponseWrapper(T body, MultiValueMap<String, String> headers, int rawStatus) {
    responseEntity = new ResponseEntity<>(body, headers, 200);
}

    public ResponseEntity build() {
         return this.responseEntity;
    }
}
 类似资料:
  • 我希望在spring boot中返回类似以下内容的json响应: 我的RestController如下所示 但我得到的反应是这样的

  • 我试图创建API与laravel护照认证现在它是很容易使API与laravel护照。我使用个人访问令牌为API用户创建访问令牌。因为我的客户端和服务器都是一个,但是当API中没有提供令牌时,它重定向到由laravel auth scaffold创建的登录页面,我想返回带有消息令牌未提供的json响应。 https://laravel.com/docs/5.4/passport#personal-访

  • RedirectAttribute:可以在Controller中return时设置跳转方式为重定向(return “redirect:要跳转的视图”),也可以在Controller中传参时传入一个RedirectAttributes类型的值,再配合flush给request中的存入数据,并且在重定向时候避免数据丢失。

  • 问题内容: 因此,在这段(简化的)代码中,当有人点击我的节点服务器时,我向另一个网站发出GET请求,并将HTML页面标题打印到控制台。工作良好: 但是,在实际应用中,用户可以指定要命中的URL。这意味着我的节点服务器可能正在下载20GB的电影文件或其他文件。不好。content- length报头没有被用来停止它,因为它并不是所有服务器都传输的。然后问题是: 我如何告诉它在收到第一个10KB之后停

  • 我想将我的url重定向到http://localhost:8080/clickbuy/product/details到http://localhost:8080/clickbuy/home。 我已经定义了header.jsp,并将其包含在所有页面中。当我从其他任何地方单击home link url时,从当前url添加/home,因此显示404错误。 如何在ModelAndView返回类型方法中使用