0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CharacterEncodingFilterのカスタマイズ。特定のURLの時にリクエストとレスポンスの文字コードを別の文字コードで強制エンコード

Posted at

web.xmlを残しつつjavaConfigで設定

web.xmlに以下を追加

web.xml
<!-- windows-31j を適用するフィルター -->
<filter>
    <filter-name>sjisEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>windows-31j</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>sjisEncodingFilter</filter-name>
    <url-pattern>/api/v1/396_x/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>sjisEncodingFilter</filter-name>
    <url-pattern>/api/v1/1321_x/*</url-pattern>
</filter-mapping>

javaConfigのクラス追加

AppInitializer.java
import java.util.EnumSet;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.ServletContext;

@Configuration
public class AppInitializer implements WebMvcConfigurer {
	
    public void onStartup(ServletContext servletContext) {
        // UTF-8 を適用するフィルター
        CharacterEncodingFilter utf8Filter = new CharacterEncodingFilter();
        utf8Filter.setEncoding("UTF-8");
        utf8Filter.setForceEncoding(true);

        FilterRegistration.Dynamic utf8FilterReg = servletContext.addFilter("CharacterEncodingFilter", utf8Filter);
        utf8FilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");

        // windows-31j を適用するフィルター
        CharacterEncodingFilter sjisFilter = new CharacterEncodingFilter();
        sjisFilter.setEncoding("windows-31j");
        sjisFilter.setForceEncoding(true);

        FilterRegistration.Dynamic sjisFilterReg = servletContext.addFilter("sjisEncodingFilter", sjisFilter);
        sjisFilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/custom-header2*", "/custom-header3/*");
    }
}

コントローラ

package com.example.helloworld4.app.welcome;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HelloController {

    @SuppressWarnings("deprecation")
    @RequestMapping("/custom-header")
    public ResponseEntity<String> getCustomHeaderResponse() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Custom-Header", "CustomValue");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8");
        //headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
        
        return new ResponseEntity<>("ああああ", headers, HttpStatus.OK);
    }
    
    @SuppressWarnings("deprecation")
    @RequestMapping("/custom-header2")
    public ResponseEntity<String> getCustomHeaderResponse2() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Custom-Header", "CustomValue");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=windows-31j");
        //headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
        
        return new ResponseEntity<>("いいい", headers, HttpStatus.OK);
    }
    
    @SuppressWarnings("deprecation")
    @RequestMapping("/custom-header3")
    public ResponseEntity<String> getCustomHeaderResponse3() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Custom-Header", "CustomValue");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=windows-31j");
        //headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
        
        return new ResponseEntity<>("ううう", headers, HttpStatus.OK);
    }
}

curl実行

C:\Users\rurur>curl -I POST http://localhost:8080/helloworld4/custom-header
curl: (6) Could not resolve host: POST
HTTP/1.1 200
X-Track: 7436fb82cf6242c6a8f10e6e30c65ee4
Custom-Header: CustomValue
Content-Type: application/json; UTF-8;charset=UTF-8
Content-Length: 18
Date: Sun, 06 Apr 2025 13:59:41 GMT

C:\Users\rurur>curl -I POST http://localhost:8080/helloworld4/custom-header2
curl: (6) Could not resolve host: POST
HTTP/1.1 200
X-Track: 2e39b3ac6f9540b1b75b79f76f50c542
Custom-Header: CustomValue
Content-Type: application/json;charset=windows-31j
Content-Length: 9
Date: Sun, 06 Apr 2025 13:59:33 GMT


C:\Users\rurur>curl -I POST http://localhost:8080/helloworld4/custom-header3
curl: (6) Could not resolve host: POST
HTTP/1.1 200
X-Track: 933bf2e270a24f7d94e8d21a07ecad1c
Custom-Header: CustomValue
Content-Type: application/json;charset=windows-31j
Content-Length: 9
Date: Sun, 06 Apr 2025 13:59:48 GMT
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?